wow, you guys are all making this way more difficult than it needs to
be. KISS!

rob's problem isn't really all that complex.  there's a polymorphic
alerts model that attaches to multiple models that are all
interrelated.  so far so good.

rob, the best place to learn about the syntax is
http://book.cakephp.org/view/78/Associations-Linking-Models-Together
and http://api.cakephp.org/class/model
i'll give you a quick rundown of what it's doing and why, but you need
to learn the section on models inside and out, backwards and
forwards.  it will save you much time in the future.

to address all of the other suggestions: they have been completely
overkill and/or off-the-mark.  this functionality is built-in and very
easy to use.

rob, the reason why you use a "different name", or define and alias
for a model, it's because you might be using the same model in the
same query more than once.  think:  if you queried account and it's
associated alerts, along with related campains and it's alerts... how
does the DB know which alerts go with with other table (campaigns/
accounts)?  thus are aliases useful and necessary.

let's go over all the bits:

public $hasMany = array(
  'AccountAlert' => array( // this is the alias.  if you follow the
convention of "current model" + "other model", AccountAlert is the
alias
    'className' => 'Alert', // this tells cake that you will be using
the Alert model, since we have specified a custom alias
    'foreignKey' => 'entity_id', // this is the foreign key in the
alerts table that we will join on
    'conditions' => array(
      'AccountAlert.model' => 'Account' // this is a field that i
assume lives in your alerts table, so you know that the entity_id
relates to
    ),
    'type' => 'LEFT' // this is totally optional, but you can specify
what type of join you want to use.  play around to find out what works
best for you
  )
);

after you've done this to all the models that are related to alerts,
then it's just a matter of doing a $model->contain() to specify how
deep you want to go, and then a $model->read() or ->find().

feel free to ping me if you need further help.

On May 4, 4:34 am, "[email protected]"
<[email protected]> wrote:
> First of all, at least to me, you will either have to use Polymorphic
> Behavior OR you cannot use "foreignKey" but need to use multiple FKs
> per model.
>
> Following example would NOT use polymorphic (but some FKs being NULL
> instead) - it uses one FK per model that is bound to Alert.
>
> Account HasMany Campain, Campain BelongsTo Account (account_id in
> campains table)
> Campain HasMany Creative, Creative BelongsTo Campain (campain_id in
> creatives table)
>
> Account HasMany Alert, Alert BelongsTo Account (account_id in alerts
> table)
> Campain HansMany Alert, Alert BelongsTo Campain (campain_id in alerts
> table)
> Creative HansMany Alert, Alert BelongsTo Creative (creative_id in
> alerts table)
>
> Make sure those are setup correcty. Setup those in your DB and use
> "cake bake" to create the models.
> If you are working in an already finished app, just separate the
> problem into a new app and bake that app.
>
> I am still not sure if this works but I would try something like this:
>
> <?php
>         $allAlerts = $this->Account->find(
>                 'all',
>                 array(
>                         'fields' => array(
>                                 'Alert.title',
>                                 'Alert.body',
>                                 'Alert.created',
>                         ),
>                         'conditions' => array(
>                                 // This selects a given Account - if you want 
> multiple try
> 'Account.id' => array(1, 2, 3), should result into IN(1,2,3)
>                                 'Account.id' => $givenAccountid,
>                                 // This does the joins
>                                 'Account.id' => 'Campain.account_id'
>                                 'Campain.id' => 'Creative.comapin_id'
>                                 // This selects all of the possible alerts
>                                 'OR' => array(
>                                         'Alert.account_id' = 'Account.id'
>                                         'Alert.campain_id' = 'Campain.id'
>                                         'Alert.creative_id' = 'Creative.id'
>                                 )
>                         )
>                         'order' => 'Alert.created DESC'
>                 )
>         );
> ?>
>
> Set debug = 2 in core.php and see if the queries do the right thing.
> If that, I am again not sure but maybe you can switch to contains this
> way:
>
> <?php
>         $allAlerts = $this->Account->find(
>                 'all',
>                 array(
>                         'fields' => array(
>                                 'Alert.title',
>                                 'Alert.body',
>                                 'Alert.created',
>                         ),
>                         // This does the joins
>                         'contain' => array(
>                                 'Campain' => array(
>                                         'Creative'
>                                 )
>                         ),
>                         'conditions' => array(
>                                 // This selects a given Account - if you want 
> multiple try
> 'Account.id' => array(1, 2, 3), should result into IN(1,2,3)
>                                 'Account.id' => $givenAccountid,
>                                 // This selects all of the possible alerts
>                                 'OR' => array(
>                                         'Alert.account_id' = 'Account.id'
>                                         'Alert.campain_id' = 'Campain.id'
>                                         'Alert.creative_id' = 'Creative.id'
>                                 )
>                         ),
>                         'order' => 'Alert.created DESC'
>                 )
>         );
> ?>
>
> I hope that helps.
>
> Jonas
--~--~---------~--~----~------------~-------~--~----~
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