微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

试图在CakePHP 2.0中保存HABTM模型数据

我有一个需要使用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,title varchar(50) NOT NULL,body text NOT NULL,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,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','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','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
                )

        )

)

我一直收到这个错误

Error: sqlSTATE[42S22]: Column not found: 1054 UnkNown column ‘QuestionsSurvey.survey_id’ in ‘where clause’
sql Query: SELECT AppModel.question_id FROM engage.questions_surveys AS AppModel WHERE QuestionsSurvey.survey_id = 13

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

Missing Database Table
Error: Table app_models for model QuestionsSurvey was not found in datasource default.

并且’with’=> ‘QuestionsSurveys’,但得到同样的错误

Missing Database Table
Error: Table app_models for model QuestionsSurveys was not found in datasource default.

我已经尝试将模型三重奏转换为hasMany直通模型(不起作用,它只是说回到HABTM).

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

我很难过.有人知道我做错了什么吗?此外,我为很长的条目和代码部分道歉,但我想确保彻底.

感谢您的时间!
马特

解决方法

您的数据应如下所示:

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

保存数据使用:
    $这 – >调查 – >白水($数据);

谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐