0
votes

I have three models in a cakephp application: ModelX, ModelY and ModelZ. The table for ModelX has the fields parent_id, lft and rght. The associations between the models are as follows:

ModelX hasOne ModelY
ModelY belongsTo ModelX
ModelY hasMany ModelZ
ModelZ belongsTo ModelY

I have a query on ModelX with joins to ModelY and ModelZ like this:

$this->ModelX->find('all', array(
            'joins' => array(
                    array(
                            'table' => 'modelys',
                            'alias' => 'ModelY1',
                            'type' => 'left',
                            'conditions' => array(
                                    'ModelX.id = ModelY1.modelx_id',
                            )
                    ),
                    array(
                            'table' => 'modelzs',
                            'alias' => 'ModelZ1',
                            'type' => 'left',
                            'conditions' => array(
                                    'ModelY1.id = ModelZ1.modely_id'
                            )
                    )
            )

));

In the result of this query, I have the data for ModelX, ModelY but NOT ModelZ, I don't know why!!. Can anybody see what is wrong with this query please?

1

1 Answers

-1
votes

I think it's nothing wrong with your query. Using model_id (convention over configuration) seems more practical, but if your setup need it, keep it then.

Just add this before your query.

$this->ModelX->recursive = 2;

On CakePHP Docs, you need to set recursion to all linked models. Be careful, this might be more slow and/or return undesired data.