Last active
December 13, 2015 16:59
-
-
Save mbischof/4944168 to your computer and use it in GitHub Desktop.
returns posts of a category via many-many-relation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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