Can't To Figure Out With Cgridview.filter

I have 2 tables:

CREATE TABLE testbase.author(

id INT (11) NOT NULL AUTO_INCREMENT,

descr VARCHAR (100) NOT NULL DEFAULT ‘<>’,

PRIMARY KEY (id)

)

and

CREATE TABLE testbase.book(

id INT (11) NOT NULL AUTO_INCREMENT,

descr VARCHAR (100) NOT NULL DEFAULT ‘<>’,

author_id INT (11) NOT NULL,

PRIMARY KEY (id)

)

I need to create view for author’s model where his books will be displayed.

In AuthorController i wrote:


public function actionView($id)

	{

		$bookModel=new Book('search');

		$bookModel->unsetAttributes();

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

			$bookModel->attributes=$_GET['Book'];


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

			'model'=>$this->loadModel($id),

			'bookModel'=>$bookModel,

			'bookDataProvider'=>$bookModel->searchByAuthor($id),

		));

	}

in Book model:


public function searchByAuthor($author_id_param)

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('descr',$this->descr,true);

		$criteria->compare('author_id',$this->author_id);

		

		$criteria->condition='author_id=:author_id';

		$criteria->params=array('author_id'=>$author_id_param);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

in view.php (Author) for displaying books I place CGridView:


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

 	'dataProvider'=>$bookDataProvider,

 	'filter'=>$bookModel,

	'columns'=>array(

		'id',

		'descr',

		array(

			'class'=>'CButtonColumn',

		),

 	)));

Everything is OK except filtering, it doesn’t work and I don’t know why, can anybody help me to solve this issue?

I am not certain what your goal is, but in the function searchByAuthor($author_id_param) the filter condition compare for auth_id is hindered by the fact that a condition for the same auth_id is added also, which explains why filtering is not working.

I’ve chnged my code but it is still not working:


public function searchByAuthor($author_id_param)

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('descr',$this->descr,true);

		//$criteria->compare('author_id',$this->author_id);

		

		$criteria->condition='author_id=:author_id';

		$criteria->params=array('author_id'=>$author_id_param);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

My goal is to make data filtering possible in CGridView which contain related data. Screenshot of the view.php in attachments.

Hi VladC#,

Modify searchByAuthor to:




public function searchByAuthor($author_id_param)

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('descr',$this->descr,true);

		$criteria->compare('author_id',$author_id_param);

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}



Or, in fact, you don’t need searchByAuthor method. Modify the controller code to:


public function actionView($id)

	{

		$bookModel=new Book('search');

		$bookModel->unsetAttributes();

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

			$bookModel->attributes=$_GET['Book'];

		$bookModel->author_id = $id;


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

			'model'=>$this->loadModel($id),

			'bookModel'=>$bookModel,

			'bookDataProvider'=>$bookModel->search(),

		));

	}



GREAT, IT WORKS! Softark thanks a lot, I’ve modified searchByAuthor as you adviced.