【问题标题】:Cake php, sql error 150蛋糕php,sql错误150
【发布时间】:2012-08-29 16:57:37
【问题描述】:

在我发布我的问题/代码之前,我将介绍一下我正在做的事情。目前我正在开发一个使用 cakephp 和 mysql 的网站。我有一个用户可以登录的登录系统,每个用户都可以创建一个团队。当用户创建团队时,他们被视为团队负责人。我的数据库中有两个表。一种用于用户,另一种用于团队。在 team 表中有一个名为 user_id 的字段,它代表创建团队的用户。它是一个外键,链接到 User 表中名为 id 的字段。

因此,当用户创建团队时,我希望我的程序自动识别领导者的 id 并将其保存为团队模型中的 user_id 进入我的表格。

这是我在团队模型类中的代码

<?php
class TeamsController extends AppController {

public $name = 'Teams';

public function add() {
    if ($this->request->is('post')) {

                    //THIS LINE SETS THE TEAM LEAD
        $this->Team->set('user_id', $this->Auth->user('id'));

        echo $this->Auth->user('id');
        if ($this->Team->save($this->request->data)) {
            $this->Team->set('user_id', $this->Auth->user('id'));
            $this->Session->setFlash('The team  has been saved');
            $this->redirect(array('controller' => 'Users','action' => 'index'));
        } else {
            $this->Session->setFlash('The team could not be created. Please, try again.');
        }

    }
}   

如您所见,我正在从当前用户模型中获取 user_id 并将其放入团队模型中。

但是,当我这样做时,我不断收到 mysql 错误 150,这与外键约束有关。我不确定为什么会这样。我检查并交叉引用了两个表中的所有数据,数据在两个表中!两个表都是 Innodb,两个字段都是整数,不带空值。

【问题讨论】:

    标签: php mysql database cakephp web-applications


    【解决方案1】:
    <?php
       class TeamsController extends AppController {
    
        public $name = 'Teams';
    
       public function add() {
        if ($this->request->is('post')) {
    
                    //THIS LINE SETS THE TEAM LEAD
        $this->request->data['Team']['user_id']= $this->Auth->user('id');
    
    
        if ($this->Team->save($this->request->data)) {
            $this->set('user_id', $this->Auth->user('id'));
            $this->Session->setFlash('The team  has been saved');
            $this->redirect(array('controller' => 'Users','action' => 'index'));
        } else {
            $this->Session->setFlash('The team could not be created. Please, try again.');
        }
    
       }
    } 
    

    【讨论】:

      【解决方案2】:

      您的代码应如下所示:

      您的控制器代码:

      <?php
      class TeamsController extends AppController {
      
      public $name = 'Teams';
      
      public function add() {
        if ($this->request->is('post')) {
          $team_details = $this->request->data;
          $team_details['Team']['user_id'] = $this->Auth->user('id');
          if ($this->Team->save($team_details)) {
              $this->Session->setFlash('The team  has been saved');
              $this->redirect(array('controller' => 'Users','action' => 'index'));
          } else {
              $this->Session->setFlash('The team could not be created. Please, try again.');
          }
        }
      }
      

      【讨论】:

      • 我们不应该在 hidden_​​field 中使用 user_id,因为我们已经在 Auth.and 安全原因。
      • set方法和你上面用的方法有什么区别?
      • 我也按照上述方法将保存的记录数组用于将来的目的。如果该字段与当前登录的 user_id 不同,您可能无法使用该字段访问 user_id。如果您没有向 save() 方法提供任何数组,set() 方法将起作用。检查此链接:book.cakephp.org/2.0/en/models/…
      • 嘿,不用开始一个新问题,我将如何通过团队模型更改用户模型中的字段?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多