Skip to content

Instantly share code, notes, and snippets.

@mbischof
Last active December 13, 2015 16:59
Show Gist options
  • Save mbischof/4944168 to your computer and use it in GitHub Desktop.
Save mbischof/4944168 to your computer and use it in GitHub Desktop.
returns posts of a category via many-many-relation
<?php
//table post (id, title)
//table category (id, name)
//table post_category (postId, categoryId)
//class Post extends CActiveRecord
public function relations()
{
return array(
'categories' => array(self::MANY_MANY, 'Category', 'tbl_post_category(postId, categoryId)')
);
}
public function search($category)
{
$criteria = new CDbCriteria;
$criteria->with = array(
'categories' => array(
'condition' => 'categories.id = :categoryId',
'params' => array(':categoryId' => $category->id),
'together' => true
)
);
return new CActiveDataProvider('Posts', array(
'criteria' => $criteria,
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment