Yii Framework Forum: Can't To Figure Out With Cgridview.filter - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Can't To Figure Out With Cgridview.filter Rate Topic: -----

#1 User is offline   VladC# 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 25-July 13

Posted 25 July 2013 - 11:55 AM

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?
0

#2 User is offline   Ronald van Belzen 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 04-July 13
  • Location:The Netherlands

Posted 25 July 2013 - 01:32 PM

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.
0

#3 User is offline   VladC# 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 25-July 13

Posted 25 July 2013 - 05:34 PM

View PostRonald_, on 25 July 2013 - 01:32 PM, said:

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.
0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,059
  • Joined: 16-February 11
  • Location:Japan

Posted 25 July 2013 - 06:19 PM

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(),
		));
	}

0

#5 User is offline   VladC# 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 25-July 13

Posted 26 July 2013 - 04:44 AM

View Postsoftark, on 25 July 2013 - 06:19 PM, said:

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users