Cgridview Adanced Search With 2 Models

Hi there,

i have a problem with the search function of CGridView, i’l try to be clear…

My models are :

Model Person :

columns

@property integer $id

@property string $name

relations

@property House[] $house

‘house’ => array(self::HAS_MANY, ‘House’, ‘idperson’),

Model House :

columns

@property integer $id

@property string $name

@property integer $idperson

relations

@property Person $idperson

‘idperson’ => array(self::BELONGS_TO, ‘Person’, ‘idperson’),

I have 3 persons, only one owns a house

when i display the list of persons in CGridView, everything is ok, i also successfully added a column in the grid to display the persons who own a house :


$this->widget('zii.widgets.grid.CGridView', array(

				'id'=>'person-grid',

				'dataProvider'=>$model->search(),

				'filter'=>$model,

				'columns'=>array(

					'id',

					'name',

					array(

						'class'=>'CCheckBoxColumn',

						'name'=>'owner',

						'header'=>'Owner',

						'selectableRows'=>0,

						'checked'=>'$data->isOwner($data->id)',

					),

					array(

						'class'=>'CButtonColumn',

						'header' => 'Action',

					),

				),

			));

In my model :


public function isOwner($id) {

		return House::model()->findAll(array("condition"=>"idperson=" . $id));

}

Then i manually added a property in the model to deal with persons who own a house :

public $owner = 0;

In my CGridView i now want to filter on those who have at least one house.

I added a checkbox (called ‘owner’) in the detailed search form, and modified my action in the Person controller :

Before :


$model=new Person('search');

$model->unsetAttributes();  // clear any default values

if(isset($_GET['Person']))

	$model->attributes=$_GET['Person'];

$this->render('Admin',array(

			'model'=>$model,

		));

After :


$model=new Person('search');

$model->unsetAttributes();  // clear any default values

if(isset($_GET['Person'])) $model->attributes=$_GET['Person'];

if(isset($_GET['Person']['owner'])) $model->owner = $_GET['Person']['owner']; 

$this->render('Admin',array(

			'model'=>$model,

		));

i tried to add this in the search method :


if ($this->owner == 1) $criteria->with = array(

			'house'=>array(

				'select'=>false,

				'joinType'=>'INNER JOIN',

			)

		);

But i noticied that “owner” was always at 0, i suppose it’s because


$model=new Person('search')

is called before


$model->owner = $_GET['Person']['owner'];

(logical…)

so i tried this :


$criteria->with = array(

			'house'=>array(

				'select'=>false,

				'joinType'=>'INNER JOIN',

			)

		);

To test if i can only display owners… (i’ll deal with the ‘owner’ problem later…)

but i doesnt work neither

The strange thing is that the number of results in CGridview seems ok, but the list not : it shows all the persons.

Any idea on how to solve this ? Thanks !