For one ‘chat’ record I have to find all ‘visitor’ records, which have any ‘message’.
I have this relation definition in Chat model class:
'visitorsWithMessages' => array(self::HAS_MANY, 'Visitor', 'chat_id',
'join' => 'JOIN (SELECT DISTINCT visitor_id FROM message WHERE chat_id = :ypl0) cm ON (cm.visitor_id = visitorsWithMessages.id)'
),
See I had to insert ‘:ypl0’ to join clause. Can this query be rewritten in better way using Yii code?
(Earlier I had
'join' => 'JOIN message ON (visitor_id = visitorsWithMessages.id)', 'group' => 'visitorWithMessages.id'
, but it was quite slow because of joining with whole ‘message’ table.)