2
votes

I'm currently working on a project with CakePHP and its find() method to handle database queries.

My current situation is as follows:

I have 3 Models: User, Location and Order. The connection is that a User hasOne Location and User hasMany Orders.

When I'm using the find() method to get the User and the address (stored in the Location model) CakePHP returns the User, Location, and the Order Models. In my case, I don't need the Order information.

So my question is: Is it possible to tell CakePHP not to join with the Order Model?

I know about the recursive attribute, but if I set it to -1, CakePHP returns just the User Model and in the case of recursive >= 0, it returns all 3 Models.

2

2 Answers

2
votes

Solved problem with Containable of cakephp. The code is as follows:

$this->User->Behaviors->load('Containable');

$this->User->recursive = -1;
$this->paginate = array('fields' => array('User.*'),
                        'contain' => array('Location'=>array('field1', 'field2')
                       );

$user = $this->Paginator->paginate('User');
1
votes

You can use unbind model function to exclude desired models

In your case :

$this->User->unbindModel(array('hasMany' => 'Orders'));

For common

$this->User->unbindModel(
            array(
                'hasMany' => array('Model1','Model2'),
                'hasOne' => array('Model1','Model2'),
                'belongsTo' => array('Model1','Model2'),
                )
        );