CActiveDataProvider and Relations

I recently updated my code to use CActiveDataProvider with CListView. I am having a problem accessing relations though. Here is what I have:

Model relation:


'rel_genres'=>array(self::HAS_MANY, 'Option', 'link_id', 'condition'=>'gen.attribute_id=4', 'alias'=>'gen'),

Controller:


$criteria=new CDbCriteria;

$criteria->with=array('user');


if(!empty($_GET['genre']))

{

	array_push($criteria->with, 'rel_genres');

	$criteria->addInCondition('gen.value', $_GET['genre']);

}


$model=Profile::model();


$dataProvider=new CActiveDataProvider($model, array(

	'criteria'=>$criteria,

	'pagination'=>array(

		'pageSize'=>16,

	),

));

View:


<?php $this->widget('zii.widgets.CListView', array(

    'dataProvider'=>$dataProvider,

    'itemView'=>'_profiles',

)); ?>

This was all working fine before I changed to CActiveDataProvider (I was using a standard findAll() before that).

The error I am getting is: Column not found: 1054 Unknown column ‘gen.value’ in ‘where clause’

My first suggestion would be to specify the alias ‘gen’ in $criteria. IIRC, at least some of the criterias declared in the relationship only takes effect on lazy loading.

/Tommy

Nope, that has not helped either.

I did a search of the forum and found this:

http://www.yiiframework.com/forum/index.php?/topic/9484-problem-with-cactivedataprovider/

It looks like the same issue - in which case it’s a problem with CActiveDataProvider.

Though I’m not too keep on using the solution posted in that topic, but would welcome any other suggestions…

This seems to have fixed the problem:

$criteria->together=true;

However I’m not sure why I didn’t need this when I was using findAll()!