改变Yii中关系的条件

时间:2013-02-21 06:06:47

标签: join yii conditional-statements has-many yii-relations

我面临一个小问题,我需要你的帮助。我想加入两个表,但是加入的其他条件不仅仅是外键。我已经尝试设置条件参数,但它在我的查询中创建了一个WHERE,这将导致我的主表将被过滤而不是已加入的主表。

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

这将是条件 - >

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

2 个答案:

答案 0 :(得分:6)

condition替换为on

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

另请参阅:http://www.yiiframework.com/forum/index.php/topic/10185-using-relations-and-conditions/

答案 1 :(得分:1)

或者例如。

相关

  

' dokumentumok' =>数组(self :: HAS_MANY,' Fileuploader',' foreign_id');

获取模型

  

YouModelName :: model() - > with(array(' dokumentumok' => array)' on' =>   "(dokumentumok.fileuploader_type =' nyomtatvany')AND   dokumentumok.fileuploader_deleted = 0"))) - > findAll();