You have to put the conditions in the association itself.

var $hasMany = array('InvoiceItem' =>
        array(
                'className' => 'InvoiceItem',
                'conditions' => 'InvoiceItem.invoice_id IS NULL',
        )
);

If you need to do this dynamically, then look at bindModel /
unbindModel (or any one of the extensions for this process, such as
Mariano's Bindable Behaviour -
http://bakery.cakephp.org/articles/view/bindable-behavior-control-your-model-bindings
);


On Dec 17, 9:00 am, the_woodsman <[EMAIL PROTECTED]> wrote:
> Hi Bakers!
>
> After a long break from development, Im back at it, and I have a
> problem querying my models.
>
> I want to get all client records, where their associated invoiceitems
> (client hasMany invoiceitems) have a particular condition.
>
> Now, If I do
>
> $this->Client->findAll("name='some name' ")
>
> All is well, my invoice table is joined in one query, and all the
> others are joined in their own queries too:
>
> SELECT `Client`.`id`, `Client`.`name`, `Client`.`address`,
> `Client`.`postcode`, `Client`.`contact`, `Client`.`email`,
> `Client`.`method`, `Client`.`paymentterms`, `Client`.`signedinvoices`
> FROM `clients` AS `Client` WHERE name='some name'               1       1     
>   0
> 2       SELECT `Invoiceitem`.`id`, `Invoiceitem`.`client_id`,
> `Invoiceitem`.`date`, `Invoiceitem`.`description`,
> `Invoiceitem`.`cost`, `Invoiceitem`.`quantity`,
> `Invoiceitem`.`invoice_id`, `Invoiceitem`.`type`,
> `Invoiceitem`.`service_category_id`, `Invoiceitem`.`user_id` FROM
> `invoiceitems` AS `Invoiceitem` WHERE `Invoiceitem`.`client_id` IN
> (230)
>
> But when I try to add conditions for the associated Invoiceitem:
>
> SELECT `Client`.`id`, `Client`.`name`, `Client`.`address`,
> `Client`.`postcode`, `Client`.`contact`, `Client`.`email`,
> `Client`.`method`, `Client`.`paymentterms`, `Client`.`signedinvoices`
> FROM `clients` AS `Client` WHERE name='some name' AND invoice_id IS
> NULL
>
> 1054: Unknown column 'invoice_id' in 'where clause'
>
> These conditions are inserted in the first query, only for the Client
> table, which of course fails.
>
> I've tried this with adding the table name, no change (1054: Unknown
> column 'Invoiceitem.invoice_id' in 'where clause'), and specifying
> with the new expects behaviour exactly which models use.
>
> What's the Cake way to add conditions for the associated model??
>
> Thanks!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
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