Re: Trying to Understand the Containable Behavior
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, "j0n4s.h4rtm...@googlemail.com" 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: > > $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: > > $allAlerts = $this->Account->find( > 'all', > array( > 'fields' => array( > 'Alert.title', > 'Alert.body', > 'Alert.created', > ), >
Re: Trying to Understand the Containable Behavior
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: 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: 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 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 -~--~~~~--~~--~--~---
Re: Trying to Understand the Containable Behavior
On May 3, 6:43 pm, Rob Wilkerson wrote: > Hey, Joshua - > > This is the first solution I've seen/found that looked like it had any > hope, but I'm not even a little bit familiar with the association > syntax you're using. I already have, in my Account model: > > public $hasMany = array ( > 'Campaign', > 'Alert' => array ( 'foreignKey' => 'entity_id' ) > ); > > It looks like you're doing something slightly different, though, that > I've never seen. Can you point me to a resource that can help me > understand it better? Is there some additional functionality added by > defining a specific name for the Alert model join? > > Thanks for your input. I'd love to be able to pull all alerts for any > account and its child items in one simple step. This looks like it > might get me there, but I'd like to understand more about what you're > suggesting. So frustrating. I feel like I'm dancing all around a final solution, but nothing quite works all the way. Maybe more code will help. As a reminder, what I'd like to do is pull all of the Alert records that are related to an Account or any Campaign that belongs to a given account. I'd like to order the Alerts returned in descending order of created date. Here are the associations defined in my Account and Campaign models: ACCOUNT: public $hasMany = array ( 'Campaign', 'Alert' => array ( 'foreignKey' => 'entity_id' ) ); CAMPAIGN: public $hasMany = array( 'Alert' => array ( 'foreignKey' => 'entity_id' ) ); ALERT: public $belongsTo = array ( 'AlertTemplate', 'Account' => array ( 'foreignKey' => 'entity_id' ), 'Campaign' => array ( 'foreignKey' => 'entity_id' ) ); And then my find, being executed from my Account model, looks like this: public function getAlerts ( $account_id ) { return $this->find ( 'all', array ( 'conditions' => array ( 'Account.id' => $account_id ), 'contain' => array ( 'Alert', 'Campaign' => 'Alert' ) ) ); } Finally, for anyone who's still with me, here is the output: Array ( [0] => Array ( [Account] => Array ( [id] => 49fca3a4-306c-4a12-9a43-a93ecbdd56cb [email] => t...@robwilkerson.otherinbox.com [firstname] => Rob [lastname] => Wilkerson [company_name] => Advertising.com [business_url] => http://test.com [obi_instrument_id] => 1001001 [ace_advertiser_id] => [adcom_advertiser_name] => [address1] => 1020 Hull Street [address2] => [city] => Baltimore [postal_code] => 0 [lu_province_id] => MD [industry_types_id] => acb923ef-235a-11de-86d3- bee43f8d046d [work_phone] => 4103353353 [approved_by] => [threshold_id] => SLAB_250 [threshold_hit_count] => 0 [last_bill_balance] => 0.00 [last_bill_date] => 1241293732 [lu_status_id] => AC_WAITING_APPROVAL [privacy_policy_approved] => 0 [created] => 1241293732 [updated] => 1241293732 ) [Alert] => Array ( [0] => Array ( [id] => 49fca3a4-00b4-4e22-be3e- a93ecbdd56cb [alert_template_id] => WELCOME [entity_id] => 49fca3a4-306c-4a12-9a43- a93ecbdd56cb [valid_from] => [valid_to] => 1243885732 [replacement_keys] => [active] => 1 [created] => 1241293732 [updated] => 1241293732 ) ) [Campaign] => Array ( [0] => Array ( [id] => 49fca3ab-23fc-4b2b-8820- a93ecbdd56cb [account_id] => 49fca3a4-306c-4a12-9a43- a93ecbdd56cb [campaign_name] => My Cherry Red Campaign [adcom_campaign_name] => [campaign_type] => CPM [lu_channel_id] => 1 [daily_budget] => 10.00 [cpm] => 1.00 [bidding_value] => [start_date] => [pause_date] => [end_date] =>
Re: Trying to Understand the Containable Behavior
On May 3, 10:55 am, jmcneese wrote: > 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); Hey, Joshua - This is the first solution I've seen/found that looked like it had any hope, but I'm not even a little bit familiar with the association syntax you're using. I already have, in my Account model: public $hasMany = array ( 'Campaign', 'Alert' => array ( 'foreignKey' => 'entity_id' ) ); It looks like you're doing something slightly different, though, that I've never seen. Can you point me to a resource that can help me understand it better? Is there some additional functionality added by defining a specific name for the Alert model join? Thanks for your input. I'd love to be able to pull all alerts for any account and its child items in one simple step. This looks like it might get me there, but I'd like to understand more about what you're suggesting. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
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 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Trying to Understand the Containable Behavior
Hello Rob, I am not sure, but I think Polymorphic Behavior might help you. It is on the bakery. You can bind that one model dynamically to other models by specifying it category (called class). I am not sure but I do not see why it should not work with containable. I'd frist try to use "complex find conditions" + Polymorphic Behavior - debug = 2 in core.php and see if the query fetches much unnecessary data. You could strip that down by specifying which fields should be returned. Hope that helps/works. Regards Jonas p.s. you could as well fetch alerts per model (3 calls) and then array_combine or Set::combine or so the results. On May 3, 1:25 am, Rob Wilkerson 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---
Re: Trying to Understand the Containable Behavior
http://bakery.cakephp.org/articles/view/quick-tip-doing-ad-hoc-joins-in-model-find On May 3, 4:40 am, Brendon Kozlowski wrote: > If you can figure out the SQL query, then it could be reverse > engineered in to a CakePHP find call. Containable is merely a > mechanism to reduce any unnecessary joins that you don't need for that > particular query; it's to help make your find() calls more efficient. > First, you'd need to discover the appropriate query syntax. For more > advanced SQL commands if you're not too versed in SQL (like myself) > you'd have to work a bit backwards. Come up with the query first, > then convert it back in to a CakePHP find() call. After awhile, > through the practice of doing that, creating properly formed find() > calls will become easier and start to allow you to use the features of > Cake as it was intended to be (such as Containable, for instance). > > The nice thing about Cake's find() calls is that they're written very > modularly, so it's very easy to see where to add, or remove certain > code to come up with almost a completely different result set with > very little effort. I think it might do some caching internally too > that the regular query() call does not do, but I'm not entirely sure > about that. The most difficult things I've found with find() are > defining something other than a LEFT join, but I believe Nate showed > how to do it somewhere, either in the Google Groups, bakery, or > somewhere else. > > On May 2, 8:54 pm, Rob Wilkerson wrote: > > > On May 2, 7:25 pm, Rob Wilkerson 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. > > > The more I read, the more I think it sounds like this isn't something > > that the Containable behavior is designed for. Although my SQL isn't > > great, I also can't think of any way - outside of subqueries - to do > > it using standard SQL, so that's probably the answer to my specific > > question. Instead, I'm wondering if there's another "Cake way" to > > solve the higher level problem. Snipped from my original message: > > > "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." > > > Any chance that such an operation is possible in a way I haven't been > > able to see/find/figure out? > > > Thanks again. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Trying to Understand the Containable Behavior
On May 2, 9:40 pm, Brendon Kozlowski wrote: > If you can figure out the SQL query, then it could be reverse > engineered in to a CakePHP find call. Containable is merely a > mechanism to reduce any unnecessary joins that you don't need for that > particular query; it's to help make your find() calls more efficient. > First, you'd need to discover the appropriate query syntax. For more > advanced SQL commands if you're not too versed in SQL (like myself) > you'd have to work a bit backwards. Come up with the query first, > then convert it back in to a CakePHP find() call. After awhile, > through the practice of doing that, creating properly formed find() > calls will become easier and start to allow you to use the features of > Cake as it was intended to be (such as Containable, for instance). > > The nice thing about Cake's find() calls is that they're written very > modularly, so it's very easy to see where to add, or remove certain > code to come up with almost a completely different result set with > very little effort. I think it might do some caching internally too > that the regular query() call does not do, but I'm not entirely sure > about that. The most difficult things I've found with find() are > defining something other than a LEFT join, but I believe Nate showed > how to do it somewhere, either in the Google Groups, bakery, or > somewhere else. > > On May 2, 8:54 pm, Rob Wilkerson wrote: > > > On May 2, 7:25 pm, Rob Wilkerson 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. > > > The more I read, the more I think it sounds like this isn't something > > that the Containable behavior is designed for. Although my SQL isn't > > great, I also can't think of any way - outside of subqueries - to do > > it using standard SQL, so that's probably the answer to my specific > > question. Instead, I'm wondering if there's another "Cake way" to > > solve the higher level problem. Snipped from my original message: > > > "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." > > > Any chance that such an operation is possible in a way I haven't been > > able to see/find/figure out? > > > Thanks again. > > --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Trying to Understand the Containable Behavior
If you can figure out the SQL query, then it could be reverse engineered in to a CakePHP find call. Containable is merely a mechanism to reduce any unnecessary joins that you don't need for that particular query; it's to help make your find() calls more efficient. First, you'd need to discover the appropriate query syntax. For more advanced SQL commands if you're not too versed in SQL (like myself) you'd have to work a bit backwards. Come up with the query first, then convert it back in to a CakePHP find() call. After awhile, through the practice of doing that, creating properly formed find() calls will become easier and start to allow you to use the features of Cake as it was intended to be (such as Containable, for instance). The nice thing about Cake's find() calls is that they're written very modularly, so it's very easy to see where to add, or remove certain code to come up with almost a completely different result set with very little effort. I think it might do some caching internally too that the regular query() call does not do, but I'm not entirely sure about that. The most difficult things I've found with find() are defining something other than a LEFT join, but I believe Nate showed how to do it somewhere, either in the Google Groups, bakery, or somewhere else. On May 2, 8:54 pm, Rob Wilkerson wrote: > On May 2, 7:25 pm, Rob Wilkerson 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. > > The more I read, the more I think it sounds like this isn't something > that the Containable behavior is designed for. Although my SQL isn't > great, I also can't think of any way - outside of subqueries - to do > it using standard SQL, so that's probably the answer to my specific > question. Instead, I'm wondering if there's another "Cake way" to > solve the higher level problem. Snipped from my original message: > > "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." > > Any chance that such an operation is possible in a way I haven't been > able to see/find/figure out? > > Thanks again. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: Trying to Understand the Containable Behavior
On May 2, 7:25 pm, Rob Wilkerson 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. The more I read, the more I think it sounds like this isn't something that the Containable behavior is designed for. Although my SQL isn't great, I also can't think of any way - outside of subqueries - to do it using standard SQL, so that's probably the answer to my specific question. Instead, I'm wondering if there's another "Cake way" to solve the higher level problem. Snipped from my original message: "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." Any chance that such an operation is possible in a way I haven't been able to see/find/figure out? Thanks again. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~--~~~~--~~--~--~---