For now, you defined the following relations :

- Account hasMany Purchase, Payment
- Purchase hasMany Payment

Right ? Now, try this :

// controllers/purchases_controller.php
function purchases_not_paid($account_id = null)
{
  // We trick the bindings
  $this->Purchase->unbindModel(array('hasMany' => array('Payment')));
  $this->Purchase->bindModel(array('hasOne' => array('Payment')));

  // Results
  $results = $this->Purchase->find('all', array('conditions' =>
array('Purchase.account_id' => $account_id, 'Payment.id' => null)));
  $this->set(compact('results'));
}

If you look at the debug, you will see that the SQL query is something
like that :

SELECT Puchase`.`id`, ....
FROM `purchases` AS `Puchase`
LEFT JOIN `payments` AS `Payment`
ON (`Payment`.`purchase_id` = `Payment`.`id`)
WHERE `Purchase`.`account_id` = 1
AND `Payment`.`id` IS NULL

On 18 août, 04:36, lauren <[EMAIL PROTECTED]> wrote:
> Hi,
>
> Using the Model::find() method seems to be preferred over using
> Model::query(). However, I can't seem to wrap my head around turning
> the following SQL query into params for the find() method:
>
> Pseudo code: select all the purchases that have not yet been paid for.
>
> SELECT p.*
> FROM accounts a, purchases p
> WHERE a.id = p.account_id AND p.id NOT IN (SELECT purchase_id FROM
> payments WHERE account_id = 1) AND a.id = 1;
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to