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 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
--
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