Start from the AccountAlert model rather than the Alert model. Use
contain as well.

$alert_ids = $this->AccountAlert->find (
        'all',
        array (
                'contain' => array(),
                'conditions'    =>   array (
                        'AccountAlert.account_id'  => $account_id
                ),
                'fields'                =>   array('alert_id'),
        )
);

You should also use contain to restrict the number of models involved
in each query. Containing an empty array (or an empty string, I can't
remember) will tell your query to not join any other models.

- James

On Apr 23, 8:45 pm, Rob Wilkerson <r...@robwilkerson.org> wrote:
> I have an Alert model, an Account model and an AccountAlert model:
>
> Alert hasMany AccountAlert
> Account hasMany AccountAlert
> AccountAlert belongsTo ( Alert, Account )
>
> AccountAlert has several other defining fields, so it has its own
> primary key in the database as well as being its own model. IOW, it's
> more than a simple linking table. It has foreign keys to the alerts
> and accounts tables (alert_id and account_id, respectively).
>
> From AccountModel, I'm trying to retrieve all of the Alerts for a
> given Account.  If I try a simple find:
>
> $this->Alert->find ( 'all' )
>
> I get each Alert and each AccountAlert that has that alert. If,
> however, I try to restrict by the account_id then I get an unknown
> column error:
>
> $alert_ids = $this->Alert->find (
>         'all',
>         array (
>                 'conditions'    =>   array (
>                         'AccountAlert.account_id'  => $account_id
>                 ),
>                 'fields'                =>   array('id'),
>         )
> );
>
> Looking at the debug SQL, no join is being created. Since the model
> associations are intact (I assume this is the case since the simple
> find() returns data for both models), should CakePHP be building the
> join automagically and, therefore, understanding the
> AccountAlert.account_id syntax in my condition?
>
> Even being relatively new to CakePHP, it still seems like I should
> have run into this before, but I can't remember having seen this. Any
> push in the right direction would be much appreciated.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to