Hi, i have 3 models (post, blogs, post_blog). Table post and blog has many to many relation and i need to find posts which are not inside some blogs.
relations for post:
public function relations()
{
return array(
'blogs'=>array(self::MANY_MANY, 'Blog', 'post_blog(post_id, blog_id)'),
);
}
relations for blog:
public function relations()
{
return array(
'posts'=>array(self::MANY_MANY, 'Post', 'post_blog(post_id, blog_id)'),
);
}
i was trying to do like that, but this solution has some problems with pagination (i need to get 10 posts per page, but have 6 or 8 in different cases)
Post::model()->ignoreBlogs(array(7, 15))->search();
functions in Post model:
public function ignoreBlogs($blogs = array())
{
$criteria = new CDbCriteria;
$criteria->with = array('blogs');
$criteria->addNotInCondition('blogs.id', $blogs);
$criteria->together = true;
$this->getDbCriteria()->mergeWith($criteria);
return $this;
}
public function search()
{
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id, true);
/* skip standart code */
$criteria->order = 't.create_date desc';
$criteria->with = array('createUser', 'updateUser', 'blogs');
$criteria->mergeWith($this->getDbCriteria());
return new CActiveDataProvider(get_class($this), array('criteria' => $criteria,
'pagination'=> array('pageSize' => 10)));
}
How can i get all posts excepting posts inside blogs with id 7 or 15? (for example =) ) Thank you for your replies =)