How to select data in many to many relation

I have a question about select data in many to many relation.

Database table like same below



Relation in Photo model

'category_r' => array(self::MANY_MANY, 'Category', 'photo_category(photo_id,category_id)'),

I want to select photo have category id = 1 and 2

$criteria = new CDbCriteria;

$criteria->with->category_r= array(

            'alias' => 'cat',

            'condition' => ' and',


return new CActiveDataProvider(get_class($this), array('criteria' => $criteria));

But it doesn’t work. Do I need to select the photo some other way ?

Try something like this:


    'condition'=>' OR' 


Thank you for your advice

This case I would like both category in one photo may be use “and” but it still doesn’t work

Do you get an error, or what?


    'condition'=>' OR' 


You should be able to do something like this:

foreach($photos as $photo){

    foreach($photo->category_r as $category){

        echo $category->name;



I am much confident that code should works, maybe with slight change according to your table structures/relation definitions.

I try

$photos = Photo::model()->with('category_r')->findAll(array(

                    'condition' => ' OR'


        foreach($photos as $photo){

            echo $photo['id'].' '.$photo['name'].'<br>';


Don’t have error and that result show a few id and name.

Next How can i do?

And i try

$photos = Photo::model()->with('category_r')->findAll(array(

                    'condition' => ' OR'


        foreach($photos as $photo){

           foreach($photo as $cat)

            echo $cat->name.'<br>';



It have not any result. (No Error)

'photocategory' => array(self::MANY_MANY, 'Category', '','on'=>'(photocategory.photo_id =  AND  photocategory.category_id = )'),

Hi, I stumbled over this topic by chance (I was actually just trying to output the results from many to many joins … I thought List View should do this automagically)

Then however I was interested in the solution of this problem. Here it goes:

(I used my tables: Task, task_questions, Question)

Model Relations:

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(

			'questions'=>array(self::MANY_MANY, 'Question', 'task_questions(taskId, questionId)'),




$criteria=new CDbCriteria;



			'questions' => array(

	//Select=false gives me all questions. It can happen that you don't get all questions when you have conditions. Fun fact: Select=false actually adds a distinct Select.

			'select' => false,



		//All questions that have 2,8 or both

		$criteria->condition = 'questions.Id in (2,<img src='' class='bbc_emoticon' alt='8)' />';

		$criteria->group = '';

                //Only tasks that have both of them

		$criteria->having = 'COUNT(DISTINCT = 2';


		//Together is important for where statements / conditions

		$criteria->together = true;


		$dataProvider=new CActiveDataProvider('Task', array('criteria' => $criteria));

And in the view, you can loop over the results (depends on how you access your dataProvider).

And this is how you get all tasks that have questions with the ids 2 and 8. You don’t get questions that have either 2 or 8. You get however, tasks that have 2, 8 and x other questions. And since I don’t want to bother the guys at StackOverflow again, I have to leave it at that. (This is actually just a MySQL problem).

You can do this with HAS::MANY THROUGH relations, too.