joinyiiconditional-statementshas-manyyii-relations

Changing the condition of relations in Yii


I'm facing a little problem and I need your help. I would like to join two tables, but there would be other conditions for the join not just the foreign key. I have tried setting condition parameter but it creates a WHERE in my query and this will cause that my primary table will be filtered and not the joined one.

'dokumentumok' => array(self::HAS_MANY, 'Fileuploader', 'foreign_id', 'joinType' => 'LEFT JOIN', 'condition' => "(dokumentumok.fileuploader_type='nyomtatvany') AND dokumentumok.fileuploader_deleted = 0"),

This would be the condition ->

'condition' => "(dokumentumok.fileuploader_type='nyomtatvany') AND dokumentumok.fileuploader_deleted = 0

Solution

  • Replace condition with on:

    'dokumentumok' => array(self::HAS_MANY, 'Fileuploader', 'foreign_id', 
      'joinType' => 'LEFT JOIN', 
      'on' => "(dokumentumok.fileuploader_type='nyomtatvany') 
      AND dokumentumok.fileuploader_deleted = 0"
    )
    

    See also: http://www.yiiframework.com/forum/index.php/topic/10185-using-relations-and-conditions/