我有以下模型和協會。Cakephp循環協會不顯示
問題(模型)有很多AnswerChoices(類名= AnswerChoice) AnswerChoice(模型)有一個FollowUpQuestion(其中類= '問題')
因此,FollowUpQuestion應該有很多AnswerChoices。
但是,當我嘗試閱讀問題對象或問題列表時,結果會在FollowUpQuestion處停止。 FollowUpQuestion不顯示AnswerChoices。
我沒有設置$ this-> model->遞歸到3,4,5 ......但無濟於事。
這是爲什麼?這個循環關聯在CakePHP中是不允許的,還是有一些特殊的設置需要設置才能使它正常工作?
SQL腳本
CREATE TABLE IF NOT EXISTS
device_questions
(id
INT(11)NOT NULL,parent_answer_id
INT(11)NOT NULL,text
文字NOT NULL,answer_type
枚舉(「選擇題」, '免費表')NOT NULL,device_question_group_id
INT(11)NOT NULL,status
枚舉( '主動', '活躍', '刪除')NOT NULL DEFAULT '主動',create_date
日期時間不NUL L,modified_date
datetime NOT NULL, PRIMARY KEY(id
) )ENGINE = InnoDB DEFAULT CHARSET = utf8;INSERT INTO
device_questions
(id
,parent_answer_id
,text
,answer_type
,device_question_group_id
,status
,create_date
,modified_date
)VALUES (29,0, '測試的多層次問題', '多項選擇',7, '有效',「 2013-03-11 01:25:43','2013-03-11 01:25:43'), (30,33,'Why no?','Multiple Choice',7,'Active',' '2013-03-11 01:25:44','2013-03-11 01:25:44'), (31,36,'你爲什麼跳過?','Free Form',7,'Active','2013-03-11 01:25:44','2013-03-11 01:25:44');CREATE TABLE IF NOT EXISTS
device_question_answer_choices
(id
INT(11)NOT NULL,device_question_id
INT(11)NOT NULL,text
文本NOT NULL,has_follow_up
TINYINT(1)DEFAULT NULL,create_date
日期時間NOT NULL,modified_date
datetime DEFAULT NULL, PRIMARY KEY(id
) )ENGINE = InnoDB DEFAULT CHARSET = utf8;INSERT INTO
device_question_answer_choices
(id
,device_question_id
,text
,has_follow_up
,create_date
,modified_date
)VALUES (32,29, '是',0, '2013年3月11日1點25分43秒',「2013-03 -11 01:25:43'), (33,29,'No',1,'2013-03-11 01:25:44','2013-03-11 01:25:44'), (34,30,'Just coz',0,'2013-03-11 01:25:44','2013-03-11 01:25:44'), (35,30,'That's它的方式',0,'2013-03-11 01:25:44','2013-03-11 01:25:44'), (36,30,'Skip',1,'2013- 03-11 01:25:44','2013-03-11 01:25:44'), (37,29,'Skip',0,'2013-03-11 01:25:44',' 2013-03-11 01:25:44'); `
。
模型
應用::用途( 'AppModel', '模型');
類DeviceQuestion延伸AppModel {
public $hasMany = array(
'AnswerChoices' => array(
'className' => 'DeviceQuestionAnswerChoice',
'foreignKey' => 'device_question_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
應用::用途( 'AppModel', '模型');
類DeviceQuestionAnswerChoice延伸AppModel {
public $hasOne = array(
'FollowUpQuestion' => array(
'className' => 'DeviceQuestion',
'foreignKey' => 'parent_answer_id',
'dependent' => false,
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
。
控制器
類DeviceQuestionsController延伸的AppController {
var $name = 'DeviceQuestions';
public $helpers = array('Js', 'Time');
var $uses = array('DeviceQuestion', 'DeviceQuestionAnswerChoice');
public function test() {
$this->DeviceQuestion->recursive = 3;
$question = $this->DeviceQuestion->read(null, 29);
$this->set('question', $question); // or echo debug($question);
}
}
。
輸出。注意FollowUpQuestion數組不顯示AnswerChoices數組(第29行)。
感謝。
感謝ADmad。你提出的公約建議是我實際上有我的課程的方式。我錯誤地提到了他們的問題。對於誤導性信息,我表示抱歉。我已更正了我的初始帖子。至於你關於FollowUpQuestion別名的第一個建議,不使用問題模型......我實際上得到問題信息,而不是它與AnswerChoices的關聯。 – 2013-03-12 06:18:50
也許共享一些代碼,顯示您的模型以及您用於獲取數據的查找調用。 – ADmad 2013-03-12 07:15:53
我添加了所有相關的代碼,包括SQL腳本。 – 2013-03-12 07:58:41