many_many relationships

I have a problem, that I have to define many to many relationships, but I don’t want get all related records.

class Post extends CActiveRecord{

public function relations()

{

return array(

‘tags’=>array(self::MANY_MANY, ‘Tag’, ‘{{tag_post}}(postId, tagId)’),

);

In table tag I have field lng and for one Post there is tags with different language.

How can I define relationship to get only tags who has lng ‘en’ ?

check this: http://www.yiiframework.com/doc/api/1.1/CActiveRecord#relations-detail

You can add ‘condition’ parameter to your relation.

‘tags’=>array(self::MANY_MANY, ‘Tag’, ‘{{tag_campaign}}(campaignId, tagId)’, ‘on’=>“tags.lng=‘en’”),

now it works, but how can I pass this parameter from Model, for example:

Post::model()->findAll($criteria);

same link as in previous post… + http://www.yiiframework.com/doc/guide/1.1/en/database.dao#binding-parameters

use ‘params’ to bind values to placeholders.

I’d use a parameterized named scope myself.

http://www.yiiframework.com/doc/guide/1.1/en/database.arr#relational-query-with-named-scopes

You’ll need to have a read of that and the bit it links too.