2013-03-11 50 views
0

我有以下模型和協會。Cakephp循環協會不顯示

問題(模型)有很多AnswerChoices(類名= AnswerChoice) AnswerChoice(模型)有一個FollowUpQuestion(其中類= '問題')

因此,FollowUpQuestion應該有很多AnswerChoices。

但是,當我嘗試閱讀問題對象或問題列表時,結果會在FollowUpQuestion處停止。 FollowUpQuestion不顯示AnswerChoices。

我沒有設置$ this-> model->遞歸到3,4,5 ......但無濟於事。

這是爲什麼?這個循環關聯在CakePHP中是不允許的,還是有一些特殊的設置需要設置才能使它正常工作?

SQL腳本

CREATE TABLE IF NOT EXISTS device_questionsid 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_questionsidparent_answer_idtextanswer_typedevice_question_group_idstatuscreate_datemodified_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_choicesid 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_choicesiddevice_question_idtexthas_follow_upcreate_datemodified_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行)。

見提前http://pastebin.com/GxA7yzs1

感謝。

回答

0

求解!

public $hasOne = array(
    'FollowUpQuestion' => array(
     'className' => 'DeviceQuestion', 
     'foreignKey' => 'parent_answer_id', 
     'dependent' => false, 
     'order' => '', 
     'limit' => '', 
     'offset' => '', 
     'exclusive' => '', 
     'finderQuery' => '', 
     'counterQuery' => '' 
) 
); 

public $hasMany = array(
    'FollowUpQuestion' => array(
     'className' => 'DeviceQuestion', 
     'foreignKey' => 'parent_answer_id', 
     'dependent' => false, 
     'order' => '', 
     'limit' => '', 
     'offset' => '', 
     'exclusive' => '', 
     'finderQuery' => '', 
     'counterQuery' => '' 
) 
); 

這就是現在返回答案的選擇我的後續問題。我還必須將遞歸設置爲5以進入5級深度。

只有一件事是我必須得到FollowUpQuestion數組的第一個索引,即使我永遠不會有超過一個。變更後

輸出:http://pastebin.com/j4JVfkCB

0

你可能還沒有配置您的協會的正確與否將模型命名爲類名/文件名作爲每由於該FollowUp_Question別名並非實際使用Question模型,而是一個AppModel實例來約定。這是一個CakePHP功能,如果它無法爲模型找到適當的文件,它將使用AppModel並將表設置爲按照慣例使用。

而且按照慣例型號名稱應該是CamelCasedSingular所以你應該Answer_Choices改爲AnswerChoice與db表answer_choices和別名FollowUp_Question應該是FollowUpQuestion

+0

感謝ADmad。你提出的公約建議是我實際上有我的課程的方式。我錯誤地提到了他們的問題。對於誤導性信息,我表示抱歉。我已更正了我的初始帖子。至於你關於FollowUpQuestion別名的第一個建議,不使用問題模型......我實際上得到問題信息,而不是它與AnswerChoices的關聯。 – 2013-03-12 06:18:50

+0

也許共享一些代碼,顯示您的模型以及您用於獲取數據的查找調用。 – ADmad 2013-03-12 07:15:53

+0

我添加了所有相關的代碼,包括SQL腳本。 – 2013-03-12 07:58:41