不能在cakephp中保存多个表 [英] cant save multiple tables in cakephp

查看:129
本文介绍了不能在cakephp中保存多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学习cakephp,已经做了相当多的。我提出这个问题的唯一原因是,在cakePHP的文档可能是错误的。
我不能看到从文档或过去stackoverflow帖子在这个问题为什么(子)Teacher表不保存user_id从(父)用户表中的id表。
我没有错误,但user_id是0在教师表中,所以它不能从用户表中拾取。
我在这两个模型上有一对一的关系。
我只是测试保存超过2个模型,我有一个用户和老师。我只需在表单中输入数据,并创建一个新用户和一个新老师,user_id是老师表中的外键。
我不喜欢问这个问题,因为有很多材料,但我只是不能看到我的问题后,在cakePHP的文档。



< a href =http://book.cakephp.org/2.0/en/models/saving-your-data.html =nofollow> http://book.cakephp.org/2.0/en/models/ save-your-data.html

  public function addteacher(){
if($ this- > request-> is('post')){
$ this-> User-> create();

}
if(!empty($ this-> request-> data)){
//我们可以保存用户数据:
//它应该在$ this-> request-> data ['User']

$ user = $ this-> User-> save($ this-> request->数据);

//如果用户已保存,现在我们将此信息添加到数据
//并保存配置文件。

if(!empty($ user)){
//新创建的用户的ID已设置
// as $ this-> User-> ID。
$ this-> request-> data ['teacher'] ['user_id'] = $ this-> User-> id; //这里是问题

//因为我们的User hasOne Profile,我们可以通过User模型访问
// Profile模型:
if($ this-> User-> Teacher-> save($ this-> request-> data))
{
$ this-> Session-> setFlash(__ 。'));
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-> '] ['user_id'] = $ this-> User-> id;



应为



$ this-> request-> data ['Teacher'] ['user_id'] = $ this-> User-> id;



资本T。模型名称总是CamelCased。



这说明不需要2个保存。您只需使用



$ this-> User-> saveAll($ this-> request-> data) code>。



它将保存用户记录和教师记录,为教师记录添加适当的外键值(假设您已在用户和教师模型之间设置正确的关联)。


I am learning cakephp and have made quite a bit already. The only reason I am asking this question is that the docs in cakePHP could be wrong. I cant see from the docs or past stackoverflow posts on this issue why the (child)Teacher table doesnt save the user_id from the id table in the (parent)User table. I get no error but the user_id is 0 in the Teacher table so it isnt picking it up from the User table. I have a one-one relationship on the 2 models. I am just testing saving over 2 models where I have a User and teacher. I simply enter data in a form and create a new User and also a new teacher with the user_id being a foreign key in the teacher table. I am loathe to ask this question as there is a lot of material on this but I just cant see my issue after following the 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)) {
        // We can save the User data:
        // it should be in $this->request->data['User']

        $user = $this->User->save($this->request->data);

        // If the user was saved, Now we add this information to the data
        // and save the Profile.

        if (!empty($user)) {
            // The ID of the newly created user has been set
            // as $this->User->id.
            $this->request->data['teacher']['user_id'] = $this->User->id; //here is the problem

            // Because our User hasOne Profile, we can access
            // the Profile model through the 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 be

$this->request->data['Teacher']['user_id'] = $this->User->id;.

Capital "T". Model names are always CamelCased.

That said there is no need for 2 saves. You can just use

$this->User->saveAll($this->request->data);.

It will save both the User record and Teacher record adding proper foreign key value for the Teacher record (assuming you have setup proper association between User and Teacher model).

这篇关于不能在cakephp中保存多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆