CakePHP update associated data -
in project have 2 related models are: company , companycomment. company hasone companycomment.
now when want update company's info through saveassociated, next way:
controller action:
public function edit($id = null){ $this->company->id=$id; $this->company->recursive = 0; if($this->request->is('post')|| $this->request->is('put')){ if($this->company->saveassociated($this->request->data)){ $this->session->setflash(__('data has been updated'), 'positive_notification'); $this->redirect(array('controller'=>'companies', 'action'=>'overview')); } else{ $this->session->setflash(__('data has not been updated. '), 'negative_notification'); } }else{ $this->request->data = $this->company->read(); } } and view following:
<?php echo $this->form->create('company', array('enctype' => 'multipart/form-data'));?> <fieldset> <?php echo $this->form->input('company.newsletter'); echo $this->form->input('companycomment.comment_1', array('label'=>__('domas comment'))); echo $this->form->input('companycomment.comment_2', array('label'=>__('sunny comment'))); ?> </fieldset> <?php echo $this->form->end(__('update'));?> here request info before saving:
array( 'company' => array( 'newsletter' => '1' ), 'companycomment' => array( 'comment_1' => 'comment1', 'comment_2' => 'comment2' ) ) but not save data. help or guidance much appreciated.
here company model relation:
var $hasone = 'companycomment'; one more remark, primary key of companycomment table company_id , not auto incremented id.
try this
public function edit($id = null){ $this->company->id=$id; $this->company->recursive = 0; <--remove line if($this->request->is('post')|| $this->request->is('put')){ //change here saveassociated saveall next if($this->company->saveall($this->request->data)){ $this->session->setflash(__('data has been updated'), 'positive_notification'); $this->redirect(array('controller'=>'companies', 'action'=>'overview')); } else{ $this->session->setflash(__('data has not been updated. '), 'negative_notification'); } }else{ $this->request->data = $this->company->read(); } } cakephp cakephp-2.3
No comments:
Post a Comment