model - cakePHP not updating a record -
i trying cakephp update record in database appose creating new one. script check "first", , if not return false, should create new record, else should update existing 1 based on id specifying. have below deliberately added $this->asset->id = 62; try force update, not work. help? (also, if can understand it's suppose do, please feel free guide me perhaps better code :) new cake:
public function index() { if($this->request->is("post")) { $aid = $this->request->data['asset']['asset_identifier']; $type = $this->request->data['asset']['asset_type_id']; $asset = $this->asset->find("first",array("fields" => array("asset.id", "asset.user_id", "asset.status_id", "asset.financial_id", "asset.insurance_id"),"conditions" => array("asset.asset_identifier" => $aid, "asset.asset_type_id" => $type))); if($asset != false) { $uid = $this->auth->user("id"); // update, depends on info: $this->asset->id = $asset['asset']['id']; foreach(array("financial_id", "insurance_id", "user_id") $value) { if($asset['asset'][$value] != 0) { $emails[] = $asset['asset'][$value]; } } if(isset($emails) && count($emails)) { $this->request->data['asset']['status_id'] = 1; $email = new cakeemail(); $email->from(array('noreply@assetchase.co.za' => 'assetchase.co.za')); $email->subject('assetchase.co.za result notification.'); foreach($emails $value) { $user = $this->user->find("first",array("fields" => array("username"),"conditions" => array("id" => $value))); $email->to($user['user']['username']); $email->send('a new notification, booyah!'); // send email username. } } if($this->auth->user("user_type_id") == 2) { $this->request->data['asset']['user_id'] = $uid; } elseif($this->auth->user("user_type_id") == 3) { $this->request->data['asset']['financial_id'] = $uid; } elseif($this->auth->user("user_type_id") == 4) { $this->request->data['asset']['insurance_id'] = $uid; } } $this->asset->id = 62; if($this->asset->saveall($this->request->data)) { $this->session->setflash("a new asset has been loaded",'success'); $this->redirect(array("controller" => "asset", "action" => "thankyou", "guest")); } else { $this->session->setflash('an error has occured.','default',array('class'=>'error')); } } $assettypes = $this->asset->assettype->find('list'); $this->set("assettypes", $assettypes); }
when call $this->asset->saveall($this->request->data) telling cake take post data , try save it. not @ $this->asset set id 62 @ all. can verify inspecting output of var_dump($this->request->data). try $this->request->data['asset']['id'] = 62 instead.
Comments
Post a Comment