這裏是我的模型:CakePHP的:findById()與中容納未返回預期協會
class Question extends AppModel {
public $hasMany = array(
'CaseQuestions' => array('className'=>'Question', 'foreignKey'=>'parent_id')
);
public $hasOne = array(
'CaseStudy' => array('className'=>'Question', 'foreignKey'=>'parent_id')
);
下面是我的控制器動作:
public function admin_delete_case_question($question_id) {
$this->Question->Behaviors->load('Containable');
$this->Question->contain(array('CaseStudy'));
$question = $this->Question->findById($question_id);
debug($question);
exit;
從上面的調試返回是這樣的:
array(
'Question' => array(
'id' => '78',
'nickname' => '',
'content' => 'sdgsdfgs',
'type' => 'CQ',
'option1' => 'sdfgsdfg',
'option2' => '',
'option3' => '',
'option4' => '',
'time' => '-1',
'difficulty' => '0.0000',
'slope' => '0.0000',
'chance' => '0',
'experiment' => false,
'created' => '2013-05-02 16:30:29',
'modified' => '2013-05-02 16:30:29',
'status' => null,
'perm_id' => '76',
'notes' => null,
'is_deleted' => false,
'answer_id' => '0',
'parent_id' => '77',
'order' => null
),
'CaseStudy' => array(
'id' => null,
'nickname' => null,
'content' => null,
'type' => null,
'option1' => null,
'option2' => null,
'option3' => null,
'option4' => null,
'time' => null,
'difficulty' => null,
'slope' => null,
'chance' => null,
'experiment' => null,
'created' => null,
'modified' => null,
'status' => null,
'perm_id' => null,
'notes' => null,
'is_deleted' => null,
'answer_id' => null,
'parent_id' => null,
'order' => null
)
)
我不明白爲什麼CaseStudy數組都是NULL,因爲當我看着那條記錄(ID 77 )在數據庫中所有的數據都是正確的。我究竟做錯了什麼?
這很有道理。我要馬上測試。不過,我想澄清一件事:CaseStudy課也是問題。換句話說,我擁有屬於同一個表/模型中記錄之間的關聯關係。也許這個問題很明顯,但我想確保它很清楚,以防需要特殊處理。 – emersonthis 2013-05-03 15:15:54
是的,我明白 - 我昨晚爲自己做了一個自己的測試環境,以驗證它對你有用,在我的測試中它按預期工作。 – jrace 2013-05-03 15:31:18
也適合我!謝謝!! – emersonthis 2013-05-03 17:16:58