Sunday, May 3, 2009

Re: Trying to Understand the Containable Behavior

it works if you associate your models correctly. in each of your
models, associate them with the Alert model, like so:

public $hasMany = array(
'AccountAlert' => array(
'className' => 'Alert',
'foreignKey' => 'entity_id',
'conditions' => array(
'AccountAlert.model' => 'Account'
)
)
);

do this with each model that has alerts, making sure to put the
correct model name in the appropriate places.

and then:

$accountModel->contain(array(
'AccountAlert',
'Campaign' => array(
'CampaignAlert',
'Creative' => 'CreativeAlert'
));
$accountModel->read(null, $account_id);


joshua
http://jmcneese.wordpress.com


On May 2, 5:25 pm, Rob Wilkerson <r...@robwilkerson.org> wrote:
> 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: