Paramount
Paramount

Reputation: 1107

Temporarily remove joins while using find (cakePHP)

I'm trying to remove the joins during a single find(). All I want is to retrieve the user's subscriptions and not all the tables/fields that they are related to them.

I have tried

$subscriptions = $this->User->find('first', 
    array(
        'fields'     => array('User.subscriptions'),
        'join'       => array(''),
        'conditions' => 
            array(
                'User.id'=> $userID
            )
));

If kept with the joins, it could potentially be a performance issue in the future.

Upvotes: 1

Views: 1724

Answers (2)

Soska
Soska

Reputation: 699

You should use the containable behavior.

In your User model (or the AppModel) add this:

var $actsAs = array('Containable');

And then, on your controller:

$this->User->contain();
$subscriptions = $this->User->find('first', array(
    'fields' => array('User.subscriptions'),
    'conditions' => array(
        'User.id' => $userID
    ),
));

The CakePHP book has more documentation http://book.cakephp.org/view/1323/Containable

Upvotes: 3

junwafu
junwafu

Reputation: 333

try to add a recursive value of -1:

$subscriptions = $this->User->find('first', array(
    'fields' => array('User.subscriptions'),
    'join' => array(''),
    'conditions' => array(
        'User.id' => $userID
    ),
    'recursive' => -1
));

A -1 recursive value will make the query retrieve only the fields from User tables without any joins.

Upvotes: 4

Related Questions