开发者

cakephp - find conditions over multiple models

开发者 https://www.devze.com 2023-01-19 02:23 出处:网络
I am trying to do a find with conditions in tw开发者_如何学Pythono models. Is this possible ? $offices = $this->User->Org->find(\'first\', array(

I am trying to do a find with conditions in tw开发者_如何学Pythono models. Is this possible ?

$offices = $this->User->Org->find('first', array(
            'conditions' => array(
                "or" => array(
                    'Org.website LIKE' => $match,
                    'Domain.domain' => $match
                )
            )
        ));

The relationship looks like this

'Domain' => array(
        'className' => 'Domain',
        'foreignKey' => 'org_id',
    ),

As a containable search

$this->User->Org->Behaviors->attach('Containable');
        $offices = $this->User->Org->find('first', array(
            'contain' => array(
                'Domain' => array(
                    'conditions' => array(
                        'Or' => array(
                        'Domain.domain' => $match
                    )
                        )
                    ),
                    'Office' => array(
                        'fields' => array('Office.id', 'Office.city')
                        )
            ),
            'conditions' => array(
                "or" => array(
                'Org.website' => $match
            )
            )
        ));

Thanks

Alex


It's possible as long as your level of recursion is set appropriately, but I highly recommend using the Containable Behavior for something like this. It makes this things trivial, readable and surgical (you get what you need and only what you need).

0

精彩评论

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