Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP: findById() with Containable not returning expected associations

Here's my model:

class Question extends AppModel {


public $hasMany = array(
    'CaseQuestions' => array('className'=>'Question', 'foreignKey'=>'parent_id')
);

public $hasOne = array(
    'CaseStudy' => array('className'=>'Question', 'foreignKey'=>'parent_id')
);

Here's the action in my controller:

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;

The debug from above returns something like this:

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

I dont' understand why the CaseStudy array is all NULL because when I look at that record (id 77) in the DB all the data is correct. What am I doing wrong?

like image 545
emersonthis Avatar asked Jan 28 '26 19:01

emersonthis


1 Answers

In your example... The Question hasOne CaseStudy; that means the Question is the parent.

Based on your data, your Question has a parent_id, so I'm assuming you actually mean that the CaseStudy is the parent and Question is the child. It looks like your association is actually backwards (normally the parent wouldn't have a parent_id).

Replace your hasOne association, with a belongsTo instead:

public $belongsTo = array(
      'CaseStudy' => array('className'=>'Question', 'foreignKey'=>'parent_id')
);

That will ensure that CaseStudy is the parent, and Question is the child.

With the way it's currently set up, CakePHP is trying to look for a CaseStudy that has a parent_id equal to your Question.id; since you don't have any data like that in your DB CakePHP finds nothing and returns null values. In reality, CaseStudy is actually the parent and the Question has a parent_id equal to CaseStudy.id

like image 164
jrace Avatar answered Jan 30 '26 11:01

jrace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!