CakePHP: 2-level JOIN with one Query

Posted by Daniel Magliola on Stack Overflow See other posts from Stack Overflow or by Daniel Magliola
Published on 2010-03-08T07:29:38Z Indexed on 2010/03/08 7:36 UTC
Read the original article Hit count: 808

Filed under:
|
|

I have the following models in CakePHP:

A Deposit belongs to an Account An Account belongs to a Customer

I want to have a list of Deposits, and I need to show the name of the customer (so I have to join through the Customer). I also need to paginate this list.

If I set Deposit->recursive = 2, I can get the Customer, however, CakePHP runs one query joining Deposit and Account, and then runs one query per each Deposit, to get the Customer.

How can I make it get both models with only one query?

I tried this, but it didn't work:

    $this->paginate = array('joins' => array(
        array(
            'table' => 'customers',
            'alias' => 'AccountCustomer',
            'type' => 'inner',
            'foreignKey' => false, 
            'conditions' => array('Account.customer_id = AccountCustomer.id')
        )
    ));

Any ideas?

Thanks!
Daniel

© Stack Overflow or respective owner

Related posts about cakephp

Related posts about join