tags:

views:

55

answers:

1

Hi,

I am trying to do a find with conditions in two 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

+2  A: 

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).

Rob Wilkerson
Thanks for your reply I tried that originally but couldn't get the or part to work. I have added what I did to the question. what im I doing wrong ?
Alex