CakePHP query returns empty array where no record found in contained belognsTo model

518 views Asked by At

We sell DNA tests where I need to link each OrderItem to Sample (via belongsTo association - OrderItem.sample_id). Code to retrieve OrderItem and associated model data:

$data = $this->OrderItem->find('first', array(
    'conditions' => array('OrderItem.id' => $id),
    'contain' => array(
        'Sample'
        )
    ));

This works fine if OrderItem.sample_id is assigned a Sample id. However, if I try to edit OrderItem to assign a Sample, where OrderItem.sample_id is currently assigned NULL in MySQL DB, I get $data = array() back, i.e. Record not found. If I remove Sample from query I get OrderItem data OK.

Anyone familiar with this issue.

1

There are 1 answers

0
Nizam On

You need to look at your Model relationship, I used to have this kind of problem, noting that the relationship between the tables is the one who make the data become null.