Alexander Yakutskiy
Alexander Yakutskiy

Reputation: 315

How can i use Yii.ActiveRecord to find unrelated records?

I have two models with relation:

class Model1 extends CActiveRecord
public function relations()
{
  return array(
    'relation' => array(self::HAS_MANY, 'Model2', 'id_model1'),
  )
}

I want view by CListView those records from Model 1, which unrelated with Model2. Obvious, I can use something like

$criteria->condition = 'id NOT IN (SELECT DISTINCT id_model1 FROM model2_tbl)'

and then send this $criteria to ActiveDataProvider. But I look for something more smart, more "yii-way" for solution. Is it exist?

Upvotes: 2

Views: 389

Answers (1)

Evgeniy Chekan
Evgeniy Chekan

Reputation: 2665

Relational query options 'joinType' = 'RIGHT OUTER' and 'condition'=>'left_table.join_field IS NULL' might do the trick.

Upvotes: 3

Related Questions