php - Updating rows over 2 tables -


i having problems updating 2 tables related models. can 1 table edited on own. have checked around docs , here , cant find simple answer yet.

what new row , not edited row.

the docs because don't have id field isn't true. can fiddle around , try working prefer conventional way need use saveassociated? method save on more 1 related table?

the user table has id field , teacher table had user_id foreign key.

now before posted checked saveall, updateall , other posts here.

the 1 models user , teacher. user hasone teacher , teacher belongsto user.

i not sure if saveall or updateall way go because doesn't talk edits in docs add new.

http://book.cakephp.org/2.0/en/models/saving-your-data.html

cakephp - updating multiple tables @ same time

view code

<?php     echo $this->form->create('user');     echo $this->form->input('user.username');   //text     echo $this->form->input('teacher.firstname');   //text     echo $this->form->input('teacher.surname');       echo $this->form->input('teacher.address');   //text     echo $this->form->input('teacher.suburb');      echo $this->form->input('teacher.phone');  echo $this->form->end('save post'); ?> 

controller code

<?php public function editteacher($id = null) {     if (!$id) {         throw new notfoundexception(__('invalid post'));     }      $post = $this->user->findbyid($id);     if (!$post) {         throw new notfoundexception(__('invalid post'));     }      if ($this->request->is(array('post', 'put'))) {         $this->user->id = $id;         if ($this->user->saveall($this->request->data)) {             $this->session->setflash(__('your techer has been updated.'));             return $this->redirect(array('controller' => 'teachers', 'action' => 'displayall'));         }         $this->session->setflash(__('unable update post.'));     }      if (!$this->request->data) {         $this->request->data = $post;     } } ?> 

my inclination need store id of record either url parameter, or hidden field in form.

 <?php        echo $this->form->create('user');      // added next line      echo $this->form->hidden('user.id');      echo $this->form->input('user.username');   //text      // added line (if necessary)      echo $this->form->hidden('teacher.id');      echo $this->form->input('teacher.firstname');   //text      echo $this->form->input('teacher.surname');        echo $this->form->input('teacher.address');   //text      echo $this->form->input('teacher.suburb');       echo $this->form->input('teacher.phone');       echo $this->form->end('save post');   ?> 

you need include record id's of items wish update, otherwise create new records.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -