3

我有一个需要使用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 子句'
SQL 查询中的未知列'QuestionsSurvey.survey_id':SELECT AppModelquestion_idengage。在questions_surveys哪里。= 13AppModelQuestionsSurveysurvey_id

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

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

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

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

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

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

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

感谢您的时间!
马特

4

2 回答 2

4

您的数据应如下所示:

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

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

谢谢

于 2013-09-23T08:31:53.287 回答
3

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

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

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

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

if ($this->Survey->QuestionsSurvey->saveAll($newdata))
于 2013-02-01T21:55:04.187 回答