How to select MANY_MANY relation by Criteria?

Hi all,

I got this code:


        $criteria=new CDbCriteria;

        $criteria->order = 'publishedBegin DESC, id DESC';

        $criteria->join = 'LEFT JOIN project_has_product ON project_has_product.project_id=t.id';

	

	$criteria->condition = "isPublic = 1 AND project_has_product.product_id = ".$this->_params['Project']['product_id'];

	#$criteria->with = array('products'=>array('condition'=>"product_id = ".$this->_params['Project']['product_id']) );


        $pages=new CPagination(Project::model()->count($criteria));

        $pages->pageSize=self::PAGE_SIZE;

        $pages->applyLimit($criteria);


        $sort=new CSort('Project');

        $sort->applyOrder($criteria);


        $this->_models = Project::model()->findAll($criteria);

        $this->_pages = $pages;

I have two tables, project and product with a MANY_MANY relation.

What I wanted to do is selecting the records, in my case all projects which belong to a certain product, by setting the criteria with attribute.

But I could only get this working with a manual join.

Is there a way to use a CDbCriteria like described with MANY_MANY relations?

Best regards,

schmunk

I’m guessing you already tried Relational Active Record… which didn’t work prior to 1.1.4 so I’d say no. But supposedly the new together should make it work (setting the with and together in the CDbCriteria). I haven’t tried it so I don’t know:

CDbCriteria together method.

“related” does the job! Thanks! :lol: