views:

44

answers:

1

I'm fairly new to cakephp and I'm having a problem with a complicated find function on my models.

I have a number of Groups each containing a number of Users, and each group can make a number of Orders. Each order consists of a number of OrderAmounts which contain an amount and a user_id (relating to a User).

I have a find which finds an Order, and returns all users in the Group relating to that Order and any OrderAmounts corresponding to that user:

    $currentOrder = $this->Order->find('first', array(
        'conditions' => array(
            'Order.group_id' => $this->Session->read("Auth.User.group_id")
        ),
        'contain' => array(
            'Group' => array(
                'User' => array(
                    'OrderAmount' => array(
                        'OrderAmountType'
                    )
                )
            )
        )
    ));

What I now want to do is to return a list of all the Users in the Group relating to the Order above who do not have a corresponding OrderAmount.

So far I have this, but I'm not sure where to put the condition to exclude users with OrderAmounts - if I put the conditions in the contains part it simply removes the OrderAmounts from the model, and if I put them in the top level conditions in the find I get an error.

    $currentOrderOutstanding = $this->Order->Group->User->find('all', array(
        'conditions' => array(
            'Group.id' => $this->Session->read("Auth.User.group_id")
        ),
        'fields' => array('User.id'),
        'contain' => array(
            'OrderAmount',
            'Group'
        )
    ));
A: 

Take a look at this example from the tutorial:

$this->User->find('all', array(
'contain'=>array(
    'Profile',
    'Account' => array(
        'AccountSummary'
    ),
    'Post' => array(
        'PostAttachment' => array(
            'fields' => array('id', 'name'),
            'PostAttachmentHistory' => array(
                'HistoryNotes' => array(
                    'fields' => array('id', 'note')
                )
            )
        ),
        'Tag' => array(
            'conditions' => array('Tag.name LIKE' => '%happy%')
        )
    )
)

));

Notice that all the conditions are inside the 'contain' array. Does it help?

bancer
This doesn't seem to work for me adding in a condition to Tag (or OrderAmount in my original example) - adding a condition to OrderAmount simply returns an empty array for OrderAmounts which don't match the conditions - it has no effect on the Users returned - This seems to go against the description in the manual but I can't see why
Loftx