CakePHP multiple model conditions in find() with HABTM

旧街凉风 提交于 2019-12-04 17:10:07

You're looking for the Containable behavior; e.g.

// app_model.php
var $actsAs = array(
    'Containable'
);

I'm assuming User extends AppModel, so Containable will be set for the child (User). Now, in your controller, conditions for the related class can be placed in the 'contain' key of the second argv of find(); e.g.

// users_controller.php
$this->User->find('first', array(
    'conditions' => array('User.id' => $userId),
    'contain' => array(
        'Item' => array(
            'conditions' => array('Item.symbol' => $itemSymbol)
        )
    )
);

Cake will find the matching User and retrieve matching Items (with the same user_id and the Item.Symbol specified. But be very careful to use 'contain' => array() when it is not required, otherwise Cake will retrieve all of the defined associations, which will tax your database.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!