开发者

Temporarily remove joins while using find (cakePHP)

开发者 https://www.devze.com 2023-02-28 18:44 出处:网络
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'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
            )
));
开发者_Go百科

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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消