Saturday, May 2, 2009

Trying to Understand the Containable Behavior

I'm trying to do something that I think is reasonably complex (and
maybe outside of what the behavior was intended to do) with the
Containable behavior and, although I seem to be dancing all around it,
I can't get it quite right. I'm hoping someone here can either tell me
I'm trying to do something that can't be done or help me get it right.

I have models for Account, Campaign and Creative. An Account hasMany
Campaign and a Campaign hasMany Creative. I have an alert model that
belongs to all of these via a "generic" entity_id foreign key. This
allows me to set an alert for any of these models and retrieve them
accordingly without having to create a bunch of separate models. So
here's the thing:

What I'd like to do is, for a given Account, retrieve all of the
alerts that are relevant to that Account - including those related to
its Campaigns and the Creatives related to the Campaigns. I think (or
maybe "hope" is more appropriate) that's possible using Containable.
Ideally, I'd like to get back an array containing the Alert object and
the object to which it belongs, but no empty objects. In other words,
don't return an Account object if the Alert is attached to a Campaign.

I've tried separately "containing" those models as well as containing
them in a nested manner. Here is the current code for the nested
containment being called from the Account model:

$alerts = $this->Alert->find (
'all',
array (
'contain' => array (
'Account' => array (
'conditions' => array ( 'Account.id' => $account_id ),
'Campaign' => array (
'conditions' => array ( 'Campaign.account_id' => $account_id )
)
)
)
)
);

Any thoughts 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+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: