Yii Framework Forum: CGridView search - Yii Framework Forum

Jump to content

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

CGridView search Rate Topic: -----

#1 User is offline   egidijusb 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 29-April 10

Posted 29 April 2010 - 06:30 AM

Hi,
i have two table, one with second relation
	
public function relations()
{
return array(
    'kat'=>array(self::BELONGS_TO, 'Kategorijos', 'kategorijos_id'),
);
}


In CGridView write:
	
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'paslauga-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		'kodas',
		'pavadinimas',
		'intervalas',
                   array(
                   'name'=>'kat.pavadinimas',
                   'value'=>'$data->kat->pavadinimas',
                
                 ),
            )

)); ?>

but not search or sort in columns 'kat.pavadinimas' not works. Sort working, when i replace 'name'=>'kat.pavadinimas', to 'name' => 'kat_id'. What is wrong :/ ?
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 29 April 2010 - 07:53 AM

For sorting on related data check this two threads:

http://www.yiiframew...w-sort-columns/
http://www.yiiframew...?showtopic=8293
Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   egidijusb 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 29-April 10

Posted 29 April 2010 - 08:22 AM

View Postmdomba, on 29 April 2010 - 07:53 AM, said:

For sorting on related data check this two threads:

http://www.yiiframew...w-sort-columns/
http://www.yiiframew...?showtopic=8293


Read this, sort ok, but how with search?
0

#4 User is offline   yoshi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 170
  • Joined: 28-February 09
  • Location:Germany

Posted 29 April 2010 - 02:15 PM

View Postegidijusb, on 29 April 2010 - 08:22 AM, said:

Read this, sort ok, but how with search?


Well, you have to do the searching by yourself inside your model->search() function, so what is the problem to add searching for a related attribute in there?

regards
0

#5 User is offline   egidijusb 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 29-April 10

Posted 30 April 2010 - 10:00 AM

View Postyoshi, on 29 April 2010 - 02:15 PM, said:

Well, you have to do the searching by yourself inside your model->search() function, so what is the problem to add searching for a related attribute in there?

regards


I add in method model->search() this line: $criteria->compare('kat.pavadinimas',$this->kat->pavadinimas, true); but not working :/

	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

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

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

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

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

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

		$criteria->compare('kat.pavadinimas',$this->kat->pavadinimas, true);

		return new CActiveDataProvider('Paslauga', array(
			'criteria'=>$criteria,
		));
	}

0

#6 User is offline   mech7 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 220
  • Joined: 26-March 09

Posted 15 July 2010 - 04:16 AM

Hmm yes how to do.. like I am doing a count so it does not have a column, but it does have a non working search :P any example would be welcome.
0

#7 User is offline   Mantus 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 29-March 10
  • Location:Ludwigsburg, Germany

Posted 16 July 2010 - 09:53 AM

I am having the same problem. Can anyone post an example for a search for attributes of related?
0

#8 User is offline   betelgeuse 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 99
  • Joined: 12-January 09

Posted 16 July 2010 - 07:07 PM

View Postegidijusb, on 30 April 2010 - 10:00 AM, said:

I add in method model->search() this line: $criteria->compare('kat.pavadinimas',$this->kat->pavadinimas, true); but not working :/

	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

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

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

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

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

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

		$criteria->compare('kat.pavadinimas',$this->kat->pavadinimas, true);

		return new CActiveDataProvider('Paslauga', array(
			'criteria'=>$criteria,
		));
	}



add to your code:

$criteria->with = 'kat';
a code snippet says more than a thousand words
0

#9 User is offline   iansyah_vox 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 14
  • Joined: 30-December 10

Posted 01 February 2011 - 05:53 AM

Try This Solution

http://www.yiiframew...ough-cgridview/

it's work 4 me
0

#10 User is offline   Cherelus Roodly 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 27-November 12

Posted 27 November 2012 - 08:20 AM

Hi,

Here's my solution:

in the view I use


<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'article-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'nom',
'prix',
'definition',
/*'mark_owner.nom_marque',*/

array( // display 'author.username' using an expression


'name'=>'marque_id',
'value'=>'$data->mark_owner->nom_marque',

),

'image',
array(
'class'=>'CButtonColumn',
),
),
)); ?>


marque_id have as value: $data->mark_owner->nom_marque

And in my model I use something like that and its works:

/**
* @return array relational rules.
*/
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(

'mark_owner'=>array(self::BELONGS_TO,'Marque','marque_id'),
'line_items'=>array(self::HAS_MANY,'LineItem','article_id')

);
}

/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'nom' => 'Nom',
'prix' => 'Prix',
'definition' => 'Definition',
'marque_id' => 'Marque',
'image' => 'Image',
);
}

public function behaviors(){
return array( 'EAdvancedArBehavior' => array(
'class' => 'application.extensions.EAdvancedArBehavior'));
}

/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.

//var_dump($this);
//exit(0);

//echo 'gggg'.$this->prix;
//exit(0);

/*->find (
array(':nom_marque'=>$this->marque_id));*/

$criteria=new CDbCriteria;
$criteria->with= 'mark_owner';
$criteria->compare('id',$this->id);
$criteria->compare('nom',$this->nom,true);
$criteria->compare('prix',$this->prix);
$criteria->compare('definition',$this->definition,true);

if(!empty($_GET['Article']))
$criteria->compare('mark_owner.nom_marque',Marque::model()->find(array('condition'=>"nom_marque like '%".$_GET['Article']['marque_id']."%'"))->nom_marque, true );


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

return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}


I think this code will help all.

Roodly



View Postegidijusb, on 29 April 2010 - 06:30 AM, said:

Hi,
i have two table, one with second relation
	
public function relations()
{
return array(
    'kat'=>array(self::BELONGS_TO, 'Kategorijos', 'kategorijos_id'),
);
}


In CGridView write:
	
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'paslauga-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		'kodas',
		'pavadinimas',
		'intervalas',
                   array(
                   'name'=>'kat.pavadinimas',
                   'value'=>'$data->kat->pavadinimas',
                
                 ),
            )

)); ?>

but not search or sort in columns 'kat.pavadinimas' not works. Sort working, when i replace 'name'=>'kat.pavadinimas', to 'name' => 'kat_id'. What is wrong :/ ?

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