gpt4 book ai didi

cakephp - 尝试在CakePHP 2.0中保存HABTM模型数据

转载 作者:行者123 更新时间:2023-12-02 02:20:38 26 4
gpt4 key购买 nike

我有一个需要使用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查询:从 AppModelquestion_id AS engagequestions_surveysAppModel = 13中选择SELECT QuestionsSurveysurvey_id


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


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


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


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


我已经尝试通过模型将模型三重奏转换为hasMany(这行不通,只是说回了HABTM)。

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

我很沮丧有人知道我在做什么错吗?另外,对于很长的条目和代码部分,我深表歉意,但我想确保内容透彻。

感谢您的时间!
马特

最佳答案

您的数据应如下所示:

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


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

谢谢

关于cakephp - 尝试在CakePHP 2.0中保存HABTM模型数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14572523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com