1
votes

I have the following relationship in my models

BasicIndexing belongsTo Applicant

Applicant hasMany Request

As such I would like to retreive the BasicIndexing model and contain the Applicant Model and an applicants corresponding request as shown in the code below

$fullCondition = array(
    'contain' => array(
        'Applicant' => array(
            'Request',
            'fields'=>array('Applicant.surname','Applicant.first_name','Applicant.id')
        )
    ),
    'conditions' =>  $conditions,
    'fields'=>array('BasicIndexing.application_date','BasicIndexing.application_number')
);
$this->loadModel('BasicIndexing');
$searchResult = $this->BasicIndexing->find('all',$fullCondition);

The problem is that the result returned into $searchResult does not contain the Request model at all. It only contains the Applicant model and ignores the Request model. I tried using a model that is not associated with Applicant and i get the warning that the model is not associated to the Applicant model.

Array
(
    [0] => Array
        (
            [BasicIndexing] => Array
                (
                    [application_date] => 2012-04-17
                    [application_number] => BIA170420124356
                )

            [Applicant] => Array
                (
                    [surname] => Kermit
                    [first_name] => Frog
                    [id] => 4f8d3b63-c2bc-48a1-9fb5-0290c982293d
                )
        )
)

Is there anything im doing wrong or there is a problem with the cake 1.3.0 release?

Any help would be highly appreciated.

Thanks.

2

2 Answers

0
votes

I think it's because of your fields array. You either need to add Request.* to the existing fields array or add a fields array to Request

So it should look like one of two examples below:

$fullCondition = array(
    'contain' => array(
        'Applicant' => array(
            'fields'=>array('Applicant.surname','Applicant.first_name','Applicant.id'),
            'Request' => array(
                'fields' => array('*')
            )
        )
    ),
    'conditions' =>  $conditions,
    'fields'=>array('BasicIndexing.application_date','BasicIndexing.application_number')
);

$fullCondition = array(
    'contain' => array(
        'Applicant' => array(
            'fields'=>array('Applicant.surname','Applicant.first_name','Applicant.id', 'Request.*'),
            'Request'
        )
    ),
    'conditions' =>  $conditions,
    'fields'=>array('BasicIndexing.application_date','BasicIndexing.application_number')
);
0
votes

I just had the same problem. Basically, contain is erratic and would return the results of an associated 'belongsTo' relationship, but not the ones of an 'hasMany'. I only needed one level of recursion, and it turns out that '1' is a special thing for recursive declaration (along with -1 and 0). So my only way to get the data I wanted was to use the clunky 'recursive' declaration, but to set it to an unnecessary high '2'.

I know it's an old question, but I just spent a whole day struggling with this, I hope to spare that to some other poor schmuck stuck with that obsolete version of cake out there..