次のように、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 関係があるため、1 つの調査に多くの質問があり、1 つの質問が多くの異なる調査に含まれる場合があります。
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 不明な列 'QuestionsSurvey.survey_id' 'where 句'
SQL クエリ: SELECTAppModel
.question_id
からengage
。questions_surveys
どこにAppModel
でもQuestionsSurvey
。survey_id
= 13
私が知る限り、すべての名前は CakePHP の標準に従っており、'with' => 'QuestionsSurvey' を使用してみましたが、次のエラーが発生しました。
データベース テーブルの欠落エラー: モデル QuestionsSurvey のテーブル app_models がデータソースのデフォルトで見つかりませんでした。
そして 'with' => 'QuestionsSurveys' ですが、同じエラーが発生します:
データベース テーブルの欠落エラー: モデル QuestionsSurveys のテーブル app_models がデータソースのデフォルトで見つかりませんでした。
モデルトリオをモデルを介して hasMany に変換しようとしました (機能しませんでした。すべて HABTM に戻ると言われました)。
私はデータにあらゆる種類の異なるフォーマットを使用しましたが(CakePHP Saving Your Data)、運もありません。
私は困惑しています。私が間違っていることを誰かが知っていますか?また、エントリとコードのセクションが非常に長くなってしまったことをお詫びしますが、徹底したかったのです。
お時間をいただきありがとうございます!
マット