I have 3 tables: Categories(id, name, alias) <-> CategoryPost(category_id,post_id) <-> Posts(id, name, content) and have defined categories to have a MANY MANY relationship with Posts and vice versa with Categories.
I want to list Posts respective to their category alias. In my Posts controller I have this:
public function actionList()
{
$criteria=new CDbCriteria;
$pages=new CPagination(Merchants::model()->count($criteria));
$pages->pageSize=self::PAGE_SIZE;
$pages->applyLimit($criteria);
$models = Categories::model()->findByAttributes(array('alias'=>'laptops'))->posts;
$this->render('list',array(
'models'=>$models,
'pages'=>$pages,
));
}
This does retrieve posts by category alias, but how do I implement this into the pagination? I’m lost for ideas
Although I have just been looking at the blog demo and seems like its implementing a similiar idea to me except it is using tags and posts.
so in my Posts Model I put
// Posts Model
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(
'categories'=>array(self::MANY_MANY, 'Categories', 'categories_posts(post_id, category_id)'),
'categoryFilter'=>array(self::MANY_MANY, 'Categories', 'categories_posts(posts_id, category_id)',
'together'=>true,
'joinType'=>'INNER JOIN',
'condition'=>'??.alias=:alias'),
);
}