1

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
tshepang
  • 12,111
  • 21
  • 91
  • 136
psychoo118
  • 23
  • 1
  • 4

2 Answers2

6

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/

Willem Renzema
  • 5,177
  • 1
  • 17
  • 24
1

Or for example.

In relation

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

And get model

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

killlinuxkill
  • 337
  • 3
  • 8