Re: 3 models and search condition
Solution After reading this: http://bakery.cakephp.org/articles/view/get-the-find-query-sql-rather-than-query-result First get the id's of the locations from the offers corresponding to a certain category $this-Offer-recursive = 0; $options = array( 'conditions' = array( 'Offer.category_id' = $id), 'fields' = 'Offer.location_id', 'recursive' = -1 ); $location_ids = $this-Offer-find(sql,$options); Than get the actual locations $location_options = array( 'conditions' = array( 'Location.id IN ('.$location_ids.')' ), 'contain' = array('Offer','LocationsPicture') ); $locations = $this-Location-find('all', $location_options); $this-set('locations',$locations); -- View this message in context: http://old.nabble.com/3-models-and-search-condition-tp28866040p28871272.html Sent from the CakePHP mailing list archive at Nabble.com. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. 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: 3 models and search condition
Ok, if you accept using two finds, then you can do it with CakePHP find types. [code] $offer_locations = $this-Offer-find( 'list', array( 'fields' = array( 'id', 'location_id' ), 'conditions' = array( 'Offer.category_id' = $id ) ) ); $locations = $this-Location-find( 'all', array( 'contain' = array( 'Offer', 'LocationsPicture' ), 'conditions' = array( 'Location.id' = $offer_locations ) ) ); [/code] That will give the same result as yours. By passing the result of the find('list'), which is an array, to the conditions in the find('all'), CakePHP will automatically assume that IN is wanted for the Location.id condition. Hope you find this helpful, and congratulation with you success :) Enjoy, John On Jun 13, 4:24 pm, viciat viciuasc...@yahoo.com wrote: Solution After reading this:http://bakery.cakephp.org/articles/view/get-the-find-query-sql-rather... First get the id's of the locations from the offers corresponding to a certain category $this-Offer-recursive = 0; $options = array( 'conditions' = array( 'Offer.category_id' = $id), 'fields' = 'Offer.location_id', 'recursive' = -1 ); $location_ids = $this-Offer-find(sql,$options); Than get the actual locations $location_options = array( 'conditions' = array( 'Location.id IN ('.$location_ids.')' ), 'contain' = array('Offer','LocationsPicture') ); $locations = $this-Location-find('all', $location_options); $this-set('locations',$locations); -- View this message in context:http://old.nabble.com/3-models-and-search-condition-tp28866040p288712... Sent from the CakePHP mailing list archive at Nabble.com. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. 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
3 models and search condition
I have 3 tables (travel agency): Categories (Mountain, Sea etc.) Locations (London, Paris etc.) Offers (id,category_id,location_id) When I select a category, I want to display the locations that are found in the offers of that category. Categories and Locations are not related. -- View this message in context: http://old.nabble.com/3-models-and-search-condition-tp28866040p28866040.html Sent from the CakePHP mailing list archive at Nabble.com. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. 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: 3 models and search condition
Ok, I understand what you want to do! So you have the three models, they are related as: Category hasMany Offer Location hasMany Offer Offer belongsTo Category Offer belongsTo Location So what is your issue? Do you have a problem with the find() statement? Enjoy, John On Jun 12, 7:44 pm, viciat viciuasc...@yahoo.com wrote: I have 3 tables (travel agency): Categories (Mountain, Sea etc.) Locations (London, Paris etc.) Offers (id,category_id,location_id) When I select a category, I want to display the locations that are found in the offers of that category. Categories and Locations are not related. -- View this message in context:http://old.nabble.com/3-models-and-search-condition-tp28866040p288660... Sent from the CakePHP mailing list archive at Nabble.com. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. 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: 3 models and search condition
Yes. I want to display the locations that are found in the offers of a particular category. When the user selects the Category with, let's say id 3, I want to display the locations that are found in the offers of that category and I don't know how. -- View this message in context: http://old.nabble.com/3-models-and-search-condition-tp28866040p28866212.html Sent from the CakePHP mailing list archive at Nabble.com. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. 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: 3 models and search condition
I suggest you study the section about retrieving data in the CakePHP book at: http://book.cakephp.org/view/1017/Retrieving-Your-Data Set the recursive parameter to 2 when you retrive the data in the Category model, like: [code] $this-find( 'all', array( 'conditions' = array( 'Category.id' = $id ), 'recursive' = 2 ) ); [/code] Try it out! Later when you are more comfortable with the find statement, take a look at the Containable behavior in the CakePHP book at: http://book.cakephp.org/view/1323/Containable Using that will make your find statements easier to work with. Enjoy, John On Jun 12, 8:15 pm, viciat viciuasc...@yahoo.com wrote: Yes. I want to display the locations that are found in the offers of a particular category. When the user selects the Category with, let's say id 3, I want to display the locations that are found in the offers of that category and I don't know how. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. 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: 3 models and search condition
Yeap, I know about the Containable behaviour. The problem with retrieving the categories is another. If I get all the categories, I'm gonna probably need to retrieve the location from the offers associated with the Category. If I have 100 offers for a category (with the same location), I'm gonna get that location a hundred times. -- View this message in context: http://old.nabble.com/3-models-and-search-condition-tp28866040p28866287.html Sent from the CakePHP mailing list archive at Nabble.com. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. 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: 3 models and search condition
A correction to part of the pseudo-code :) [code] 'Offer' = array( 'Offer.category_id' = $id ) [/code] should be: [code] 'Offer' = array( 'conditions' = array( 'Offer.category_id' = $id ) ) [/code] You can attach a condition on the related models! Enjoy, John On Jun 12, 8:41 pm, John Andersen j.andersen...@gmail.com wrote: You should be able to make a find with conditions on the category even if you are retrieving the data from the Location model. Something like: [code] find( 'all', array( 'contain' = array( 'Offer' = array( 'Offer.category_id' = $id ) ) ) ); [/code] That should give you all Location records for the Offers with category_id equal the requested id! The code is not tested! Enjoy, John On Jun 12, 8:29 pm, viciat viciuasc...@yahoo.com wrote: Yeap, I know about the Containable behaviour. The problem with retrieving the categories is another. If I get all the categories, I'm gonna probably need to retrieve the location from the offers associated with the Category. If I have 100 offers for a category (with the same location), I'm gonna get that location a hundred times. -- View this message in context:http://old.nabble.com/3-models-and-search-condition-tp28866040p288662... Sent from the CakePHP mailing list archive at Nabble.com. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. 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: 3 models and search condition
Tried that: $locations = $this-Location-find( 'all', array( 'contain' = array( 'Offer' = array( 'conditions' = array('Offer.category_id' = $id) ) ) ) ); Doesn't work -- View this message in context: http://old.nabble.com/3-models-and-search-condition-tp28866040p28866584.html Sent from the CakePHP mailing list archive at Nabble.com. Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions. 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