Yii:限制HAS_MANY关系

Simple story. I have users and blog-posts, users are related to blog-posts as ONE to MANY. I would like to show users their profile along with the 5 most recent posts they wrote:

/**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'posts' => array(self::HAS_MANY, 'BlogPost', 'userId')
        );
    }

I am trying to apply limit:

$user = User::model()->with(array(
            'posts' => array('order' => 'updatedAt DESC'),
            ->findAll(array(
                'condition' => 'userId = :userId AND ...',
                'params' => array(
                    'userId' => $userId
                ),
                'limit' => 5
            ));

But Yii framework ignores that. How can I accomplish it?

It's Yii 1.1.

You have to include the limit in the with:

$user = User::model()
    ->with(array(
        'posts' => array(
            'order' => 'updatedAt DESC',
            'limit' => 5
        )
    ))->findAll(array(
        'condition' => 'userId = :userId AND ...',
        'params' => array(
            'userId' => $userId
        ),

    ));

The relations() function should look like this:

return array(
  'posts' => array(self::HAS_MANY, 'BlogPost', 'userId'),
  'recentPosts' => array(self::HAS_MANY, 'BlogPost', 'userId',
    'order' => 'updatedAt DESC',
    'limit' => 5
  )
);

calling $user->posts you will get all posts, calling $user->recentPosts will get only last 5.