3

これが私のモデルです:

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
    )
)

DB でそのレコード (id 77) を見ると、すべてのデータが正しいため、CaseStudy 配列がすべて NULL である理由がわかりません。私は何を間違っていますか?

4

2 に答える 2

2

代わりにこれを試してください:

//Question model
public $actsAs = array('Containable'); //personally, I put this in my AppModel

//QuestionsController
$question = $this->Question->find('first', array(
   'conditions' => array(
        'id' => $question_id
    ),
    'contain' => array(
        'CaseStudy'
    )
));
于 2013-05-02T20:57:00.587 に答える