【问题标题】:Trying to save HABTM model data in CakePHP 2.0尝试在 CakePHP 2.0 中保存 HABTM 模型数据
【发布时间】:2013-01-12 09:51:15
【问题描述】:

我有一个需要使用HABTM的数据模型,如下:

surveys 
(
    id int(11) NOT NULL AUTO_INCREMENT,
    user_id int(11) NOT NULL,
    title varchar(50) DEFAULT NULL,
    created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    modified timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
    PRIMARY KEY (id),
    KEY user_id (user_id)
);

questions 
(
    id int(11) NOT NULL AUTO_INCREMENT,
    user_id int(11) NOT NULL,
    title varchar(50) NOT NULL,
    body text NOT NULL,
    created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    modified timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
    PRIMARY KEY (id),
    KEY user_id (user_id)
);

questions_surveys  
(  
    id int(11) NOT NULL AUTO_INCREMENT,  
    survey_id int(11) NOT NULL,  
    question_id int(11) NOT NULL,  
    created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,  
    modified timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',  
    PRIMARY KEY (id),  
    KEY survey_id (survey_id),  
    KEY question_id (question_id)  
);

以及相关的外键:

ALTER TABLE questions_surveys
ADD CONSTRAINT questions_surveys_ibfk_1 FOREIGN KEY(survey_id) REFERENCES surveys(id);

ALTER TABLE questions_surveys
ADD CONSTRAINT questions_surveys_ibfk_2 FOREIGN KEY(question_id) REFERENCES questions(id);

问题和调查具有 HABTM 关系,因此一项调查包含多个问题,而一个问题可以包含在许多不同的调查中。

在 Survey.php 中:

public $hasAndBelongsToMany = array(
    'Question' => array(
        'className' => 'Question',
        'joinTable' => 'questions_surveys',
        'foreignKey' => 'survey_id',
        'associationForeignKey' => 'question_id'
    )
);

在 Question.php 中:

public $hasAndBelongsToMany = array(
    'Survey' => array(
        'className' => 'Survey',
        'joinTable' => 'questions_surveys',
        'foreignKey' => 'question_id',
        'associationForeignKey' => 'survey_id'
    )
);

这是我在 SurveysController.php 中添加的控制器:

public function add()
{
    $this->set('fields', $this->Survey->getFields());
    $this->set('users', $this->Survey->User->find('list', array('fields' => array('id', 'username'))));
    $this->set('questions', $this->Question->find('list', array('fields' => array('id', 'body'))));

    if (!empty($this->data))
    {
        $this->Survey->saveAll($this->data['Survey']);

        foreach($this->data['Question']['id'] as $question_id)
        {
            $newdata[] = array('Survey' => array('id' => $this->Survey->getInsertID()), 'Question' => array('id' => $question_id));
        }

        if ($this->Survey->saveAll($newdata))
        {
            $this->Session->setFlash('The survey was successfully added!');
            $this->redirect(array('action'=>'index'));
        }
        else
        {
            $this->Session->setFlash('Unable to add survey.');
        }
    }
}

首先保存新调查,然后将每个 question_survey 添加到一个数组中,然后一次添加所有这些。数据如下所示:

Array
(
    [0] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 1
                )

        )

    [1] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 2
                )

        )

    [2] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 3
                )

        )

    [3] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 4
                )

        )

    [4] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 5
                )

        )

    [5] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 6
                )

        )

    [6] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 7
                )

        )

    [7] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 8
                )

        )

    [8] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 9
                )

        )

    [9] => Array
        (
            [Survey] => Array
                (
                    [id] => 17
                )

            [Question] => Array
                (
                    [id] => 10
                )

        )

)

我不断收到此错误:

错误:SQLSTATE[42S22]:未找到列:1054“where 子句”中的未知列“QuestionsSurvey.survey_id”
SQL 查询:SELECT AppModel.question_id FROM engage.questions_surveys AS AppModel WHERE QuestionsSurvey.survey_id = 13

据我所知,一切都是按照 CakePHP 标准命名的,我尝试使用 'with' => 'QuestionsSurvey',但收到此错误:

缺少数据库表 错误:在数据源默认值中找不到模型 QuestionsSurvey 的表 app_models。

和 'with' => 'QuestionsSurveys',但得到同样的错误:

缺少数据库表 错误:在数据源默认值中找不到模型 QuestionsSurveys 的表 app_models。

我尝试将模型三重奏转换为 hasMany through 模型(没有用,只是说回到 HABTM)。

我对数据(CakePHP Saving Your Data) 使用了各种不同的格式,但也没有运气。

我被难住了。有人知道我做错了什么吗?另外,我为非常长的条目和代码部分道歉,但我想确保是彻底的。

感谢您的宝贵时间!
马特

【问题讨论】:

    标签: cakephp save cakephp-2.0 add has-and-belongs-to-many


    【解决方案1】:

    您的数据应如下所示:

    array(
        'Survey' => array(
            'title' => 'example',
        ),
        'Question' => array(
            (int) 0 => '1',
            (int) 1 => '2',
        )
    )
    

    保存数据使用: $this->Survey->saveAll($data);

    谢谢

    【讨论】:

      【解决方案2】:

      所以事实证明这本书是错误的,或者我误解了它或其他什么,这是有效的格式:

      Array
      (
          [0] => Array
          (
              [survey_id] => 33
              [question_id] => 9
          )
      
          [1] => Array
          (
              [survey_id] => 33
              [question_id] => 10
          )
      )
      

      这就是它需要保存在控制器中的方式:

      if ($this->Survey->QuestionsSurvey->saveAll($newdata))
      

      【讨论】:

      • 没有其他工作!你能告诉我正确的格式吗?这一直在踢我,因为我一直在尝试通过模型开始使用 hasMany...
      • 看看下面的答案!
      猜你喜欢
      • 2011-09-14
      • 2013-09-24
      • 1970-01-01
      • 2012-08-08
      • 2010-12-18
      • 1970-01-01
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      相关资源
      最近更新 更多