我需要通过删除表单的现有数据来保存新编辑的数据,但这会产生错误,因为在提交编辑的代码时无法保存用户bt我需要在提交表单时更新字段
public function provider_edit($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is(array('post', 'put'))) {
$name =$this->request->data['User']['profile_photo']['name'];
$tmp_name=$this->request->data['User']['profile_photo']['tmp_name'];
$filename = WWW_ROOT.'providers_photo/'.$name;
$random_number = $this->Useful->random_code();
if(file_exists(WWW_ROOT.'providers_photo/'.$name))
{
$name= $random_number.$this->request->data['User']['profile_photo']['name'];
}
$this->request->data['User']['profile_photo'] = $name;
if ($this->User->save($this->request->data)) {
$user_Id = $id;
for($i=1;$i<=$this->request->data['User']['rate_count'];$i++){
$rate_insert = $this->request->data['User']['rate_'.$i];
$rateId = $this->request->data['User']['rate_id_'.$i];
if(!empty($rate_insert)){
$this->User->query("insert into service_provider_rates
(user_id,rate_package_id,rate) values ('$user_Id','$rateId','$rate_insert')");
}
}
for($i=1;$i<=$this->request->data['User']['category_count'];$i++){
$categories = $this->request->data['User']['category_id_'.$i];
if($categories>0){
$this->User->query("insert into provider_service_categories
(user_id,service_categories_id) values ('$user_Id','$categories')");
}
}
if (move_uploaded_file($tmp_name,WWW_ROOT.'providers_photo'.'\\'.$name))
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
$this->data = $this->User->read(null, $id);
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
}
$ratePackages = $this->User->query("select RP.*,SPR.rate from rate_packages RP left join service_provider_rates SPR on SPR.rate_package_id=RP.id left join users U on U.id=SPR.user_id and U.id={$id} where RP.id is not null group by RP.id");
$this->loadModel('ServiceCategory');
$serviceCategories = $this->ServiceCategory->generateTreeList(null,null,null,' ');
$this->set(compact('categories','serviceCategories','ratePackages'));
$featureOptions = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$features = $this->User->find('first', $featureOptions);
$this->set(compact('features','user_details'));
$this->render('provider_edit');
}
需要通过删除现有的数据来保存给定ID的新编辑数据!!!