Re: Model Association Not Creating Query Joins
On Apr 25, 9:55 am, James K wrote: > 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. Thanks, everyone. Has it always been like this? For some reason, I would have sworn that in the past I've done something like this: $this->AccountAlert->find ( 'all', array ( 'conditions' => array ( 'AccountAlert.active' => 1 ), 'order' => 'Alert.priority' ) ); Note the different models being used in the conditions and order parameters. Has that never been possible? Thanks again for the help. I'll definitely be digging into the containable behavior. --~--~-~--~~~---~--~~ 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: Model Association Not Creating Query Joins
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 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 -~--~~~~--~~--~--~---
Re: Model Association Not Creating Query Joins
In cases like what you are looking at it tends to work a bit better if you rather do the find on the model you want to filter. Something like: $alert_ids = $this->Alert->AccountAlert->find ( 'all', array ( 'conditions'=> array ( 'AccountAlert.account_id' => $account_id ), 'fields'=> array('Alert.id'), ) ); That might work (Sorry, sitting at university, can't test). Otherwise you can try the containable behaviour, that should be able to do what you wish. On Apr 24, 2:45 am, Rob Wilkerson 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 -~--~~~~--~~--~--~---