Problem with CActiveDataProvider and related model

I am using a CActiveDataProvider to power a CGridView.

The main model for the CActiveDataProvider has a HAS_MANY relationship to another model.

I’m having two problems.

The first is that the model is only returning one line, After looking at the MySql log I can see that this is because DISTINCT is being used on the primary key of the main model in count query. The variation is in the related model. (I’ve tried adding “‘distinct’ => false” to the criteria).

The second problem is that I can’t reference the related model columns.

some code…

The main model relationship




	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'class' => array(

				self::HAS_MANY, 

				'ClassModel', 

				'package_id',

				'joinType' => 'INNER JOIN',

			),

		);

	}



The function to return the CActiveDataProvider




	public static function getOne($package_name)

	{

		

		return new CActiveDataProvider(new PackageModel, array(

			'criteria' => array(

				'with' => array(

					'class' => array(

						'select' => 'class.name AS class_name, short_description',

						'together' => true,

						'sort' => array(

							'defaultOrder' => 'class_name',

						),

					)

				),

				'condition' => 't.name = :package_name',

				'params' => array(

					':package_name' => $package_name,

				),

			),

			'pagination' => array('pageSize' => '100'),

		));

	}



Extract from the CGridView




	'columns' => array(

		array(

			'class'=>'CLinkColumn',			

			'labelExpression' => '$data->class->class_name',

			'header' => 'Class',

			'urlExpression' => '"/' . $package_name . '/" . $data->class->name',

		),

	...



The sql that is generated




88 Query	SELECT COUNT(DISTINCT `t`.`package_id`) FROM `package` `t`  INNER JOIN `class` `class` ON (`class`.`package_id`=`t`.`package_id`)  WHERE (t.name = 'saltnet_php_docs')

88 Query	SELECT `t`.`package_id` AS `t0_c0`, `t`.`name` AS `t0_c1`, `t`.`language_id` AS `t0_c2`, class.name AS class_name, `class`.`short_description` AS `t1_c3`, `class`.`class_id` AS `t1_c0` FROM `package` `t`  INNER JOIN `class` `class` ON (`class`.`package_id`=`t`.`package_id`)  WHERE (t.name = 'saltnet_php_docs') LIMIT 100



I solved this by changing the CActiveDataProvider to reference the class model first - this way the join was a BELONGS_TO and the DISTINCT clause did not get in the way. I would still like to know why I can’t use a HAS_MANY join in a CActiveDataProvider though.