Search code examples
yiiuniquecriteriayii-components

Yii Criteria with "with" yields duplicates


This is my Criteria:

$criteria = new CDbCriteria();
$criteria->with = array('userUrls');
$criteria->together = true;
$criteria->compare('userUrls.community_id',Yii::app()->params['currentCommunity']->id);
$criteria->order = 't.weight DESC, t.id DESC';
$urls = Url::model()->findAll($criteria);

I am basically trying to do a simple JOIN via a MANY_MANY. Except in the JOIN table I need an additional condition (that the community_id matched the given one).

Without $criteria->together = true; it fails in the WHERE statement.

If I add $criteria->distinct = true; it still gives me duplicates because other fields in the JOIN table make them technically 'UNIQUE'.

I want the results to be UNIQUE based on the URL fields, not the userUrls fields.


Solution

  • Adding $criteria->group = 't.id'; fixed it all. Which makes sense.