You would probably be better off having a countPosts() function in your User model which is...

public function countPosts( $userId ){
$count = $this->Post->find('count', array( 'condition' => array('user_id' => $userId) ) );
    return $count;
}

That way you can add in more conditions later, there is a boolean 'published' flag on the post for example,
your controller then calls $count = $this->User->countPosts($userId);

Counting array elements will work in this case, but if you want to further refine the counted records later, it will get messy and you are duplicating functionality that the model already provides.

Steve (Ratty)


On 18/06/12 23:24, JonStark wrote:
Sorry, incomplete :

It will produce an array like :

array(
        'User' =>  array(
                'password' =>  '*****',
                'id' =>  '2',
                'username' =>  'test',

        ),
        'Post' =>  array(
                (int) 0 =>  array(
                        'id' =>  '36',
                        'user_id' =>  '2', (...)

                )
        ),

How can I display the number of Posts this user have ? (in this example 1) ?

Thanks a lot.

Le mardi 19 juin 2012 00:21:25 UTC+2, JonStark a écrit :

    Let's say USer hasMany Post.

    If i debug my user it will produce an array like :

    User

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

Reply via email to