@Daniel: Glad u got it working @Andras: In the above example you do use contain so we are all on the same page, but you should not run the find on Post if looking for ONE Category and it's MANY Posts (if any exist). Your method will return the category record for every post found and to echo the category name in the view you would need to echo $categories[0]['Category']['name'] which is not very clever, and worse still if the category does not yet have any posts the category data will not be returned.
The method Jeremy suggests returns just one Category with its related Posts (if any exist) along with their related User all in the right context for the Category view. However, if your paginating the posts, then you would then have to run paginate on the Post model. @Jeremy: You, give up? No! On Nov 5, 6:58 pm, Andras Kende <[email protected]> wrote: > Paul, > > Sorry for confusion. > > Daniel first question was to display posts on a page (posts belongs to > category and user) > > I suggested earlier its better to start the query from post instead from > category, so you could get > anything from the 3 tables with a single query.. > > If there are more related models in posts table you can add contain like: > > $categories = $this->Category->Post->find('all', array( > 'fields' => array( > 'Category.name', > 'Post.id', > 'Post.comment', > 'User.username', > ), > 'conditions' => array('Category.id' => $id), > 'contain' => array( > 'Category', 'User' > ) > )); > > Also its resulting a cleaner array, I guess its easier to paginate as well.. > > Andras Kende > > On Nov 5, 2011, at 5:59 AM, WebbedIT wrote: > > > > > > > > > @Andras: Have you read the rest of the thread? Jeremy is trying to > > show Daniel how to use containable so he can specifically say what > > models and fields he wants to fetch during his find call. It's very > > unhelpful you chiming in 25 messages into the thread telling him to > > pull the model and ALL it's unnecessary related model data (a point > > that was covered earlier in the thread). > > > @Daniel: Jeremy has given you all the correct information. The > > following will pull the category matching $id and all it's matching > > Posts and their Users (if you have your relationships set correctly. > > > $this->Category->find( > > 'first', > > array( > > 'conditions' => array('Category.id' => $id), > > 'Contain' => array( > > 'Post' => array( > > 'User' => array( > > 'User.id', > > 'User.name' > > ) > > ) > > ) > > ) > > ) > > > If you're still having problems understanding this please go back to > > the cookbook and follow what it tells you step by step, there is > > nothing in this thread which is not covered there as this is basic > > stuff: > >http://book.cakephp.org/view/1323/Containable > > > HTH, Paul. > > > On Nov 4, 11:06 pm, Andras Kende <[email protected]> wrote: > >> Daniel, > > >> The easiest way to do what you need is: > > >> <?php > >> class CategoriesController extends AppController { > > >> public function index($id = null) { > >> $posts = $this->Category->Post->find('all', array( > >> 'conditions' => array('Category.id' => $id), > >> )); > >> // print_r($posts); to debug... > >> $this->set(compact('posts')); > > >> } > >> } > > >> This will just do a single query with 2 left joins.. > >> Just make sure all 3 models has the relationships setup correctly.. > > >> Andras Kende > > > -- > > Our newest site for the community: CakePHP Video > > Tutorialshttp://tv.cakephp.org > > Check out the new CakePHP Questions sitehttp://ask.cakephp.organd help > > others with their CakePHP related questions. > > > To unsubscribe from this group, send email to > > [email protected] For more options, visit this group > > athttp://groups.google.com/group/cake-php -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php
