简体   繁体   English

无法在cakephp中保存多个表

[英]cant save multiple tables in cakephp

I am learning cakephp and have made quite a bit already. 我正在学习cakephp,并且已经做了很多工作。 The only reason I am asking this question is that the docs in cakePHP could be wrong. 我问这个问题的唯一原因是,cakePHP中的文档可能是错误的。 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. 我从文档或以前的stackoverflow帖子中看不到关于此问题的原因,为什么(child)Teacher表未将(parent)User表中的id表中的user_id保存。 I get no error but the user_id is 0 in the Teacher table so it isnt picking it up from the User table. 我没有收到任何错误,但Teacher表中的user_id为0,因此它不会从User表中拾取它。 I have a one-one relationship on the 2 models. 我在2个模型上有一对一的关系。 I am just testing saving over 2 models where I have a User and teacher. 我只是测试保存有我的用户和老师的2个模型。 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. 我只是简单地以表格形式输入数据,然后创建一个新用户和一个新老师,其中user_id是教师表中的外键。 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. 我讨厌问这个问题,因为有很多材料,但是在cakePHP中的文档之后,我只是看不到我的问题。

http://book.cakephp.org/2.0/en/models/saving-your-data.html 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-> 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". 大写字母“ T”。 Model names are always CamelCased. 型号名称始终为CamelCased。

That said there is no need for 2 saves. 也就是说,无需保存2次。 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). 它将保存用户记录和教师记录,并为教师记录添加适当的外键值(假设您在用户和教师模型之间设置了正确的关联)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM