php - cant save multiple tables in cakephp -


i learning cakephp , have made quite bit already. reason asking question docs in cakephp wrong. cant see docs or past stackoverflow posts on issue why (child)teacher table doesnt save user_id id table in (parent)user table. no error user_id 0 in teacher table isnt picking user table. have one-one relationship on 2 models. testing saving on 2 models have user , teacher. enter data in form , create new user , new teacher user_id being foreign key in teacher table. loathe ask question there lot of material on cant see issue after following docs in cakephp.

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

    public function addteacher() {    if ($this->request->is('post')) {             $this->user->create();          }       if (!empty($this->request->data)) {         // can save user data:         // should in $this->request->data['user']          $user = $this->user->save($this->request->data);          // if user saved, add information data         // , save profile.          if (!empty($user)) {             // id of newly created user has been set             // $this->user->id.             $this->request->data['teacher']['user_id'] = $this->user->id; //here problem              // because our user hasone profile, can access             // profile model through user model:              if ($this->user->teacher->save($this->request->data))              {                  $this->session->setflash(__('your post has been saved.'));                   return $this->redirect(array('action' => 'login'));                 }         }       }       }   <?php   echo $this->form->create('user');   echo $this->form->input('user.username');  echo $this->form->input('user.password');    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'); ?>

$this->request->data['teacher']['user_id'] = $this->user->id;

should

$this->request->data['teacher']['user_id'] = $this->user->id;.

capital "t". model names camelcased.

that said there no need 2 saves. can use

$this->user->saveall($this->request->data);.

it save both user record , teacher record adding proper foreign key value teacher record (assuming have setup proper association between user , teacher model).


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 -