Yii Framework Forum: Search() & Filter of a relations field through CGridView - Yii Framework Forum

Jump to content

  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  • You cannot start a new topic
  • You cannot reply to this topic

Search() & Filter of a relations field through CGridView Rate Topic: ***** 11 Votes

#1 User is offline   jaxyeh 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 23-February 10

Posted 04 May 2010 - 12:58 PM

Yii Community,

I have been searching all over the Internet, but still couldn't figure out how to setup search and filter capabilities for a relationship field through CGridView... Here is example of data structure:

Here is snippet example of User Model:
	public function rules()
	{
		return array(
			array('name', 'required'),
			array('org_id', 'numerical', 'integerOnly'=>true),
			array('created_date', 'type', 'type'=>'date'),
			array('created_date, org.org_name', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('name, org.org_name, created_date', 'safe', 'on'=>'search'),
		);
	}
	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(
			'org' => array(self::BELONGS_TO, 'Organization', 'org_id'),
		);
	}
	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('org.org_name',$this->org->org_name,true); // That didn't work

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

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


Here is example of User Admin View:
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'calendar-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
	array( 
			'name'=>'name',
			'type'=>'raw',
			'value'=>'CHtml::link(CHtml::encode($data->name), array("event/view", "id"=>$data->id))',
		),
		array(
			'name'=>'org.org_name', //this lookup works
    // I need solution here to do filter & search of this relationship data field
		),
		array(
            'name'=>'created_date',
            //'type'=>'date',
            'filter'=>false,
        ),
		array(
			'class'=>'CButtonColumn',
		),
	),
)); ?>



I had searched this topic over this forum, found similar discussion without direct solution:


Hopefully someone can help me to pinpoint the RIGHT solution to setup AJAX search & filter of a relation field through CGridView.

Thanks,
Jason
2

#2 User is offline   kitty10 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 139
  • Joined: 13-April 10

Posted 05 May 2010 - 03:37 AM

View Postjaxyeh, on 04 May 2010 - 12:58 PM, said:

Yii Community,

I have been searching all over the Internet, but still couldn't figure out how to setup search and filter capabilities for a relationship filed through CGridView... Here is example of data structure:

Here is snippet example of User Model:
	public function rules()
	{
		return array(
			array('name', 'required'),
			array('org_id', 'numerical', 'integerOnly'=>true),
			array('created_date', 'type', 'type'=>'date'),
			array('created_date, org.org_name', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('name, org.org_name, created_date', 'safe', 'on'=>'search'),
		);
	}
	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(
			'org' => array(self::BELONGS_TO, 'Organization', 'org_id'),
		);
	}
	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('org.org_name',$this->org->org_name,true); // That didn't work

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

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


Here is example of User Admin View:
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'calendar-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
	array( 
			'name'=>'name',
			'type'=>'raw',
			'value'=>'CHtml::link(CHtml::encode($data->name), array("event/view", "id"=>$data->id))',
		),
		array(
			'name'=>'org.org_name', //this lookup works
    // I need solution here to filter & search the relationship
		),
		array(
            'name'=>'created_date',
            //'type'=>'date',
            'filter'=>false,
        ),
		array(
			'class'=>'CButtonColumn',
		),
	),
)); ?>



I had searched this topic over this forum, found similar discussion without direct solution:


Hopefully someone can help me to pinpoint the RIGHT solution to setup AJAX search & filter of a relation field through CGridView.

Thanks,
Jason


If i dont't have the search() method implemented in my version of yii..how can create that?
I should create an action in the controller class????
-3

#3 User is offline   jaxyeh 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 23-February 10

Posted 05 May 2010 - 06:53 AM

View Postkitty10, on 05 May 2010 - 03:37 AM, said:

If i dont't have the search() method implemented in my version of yii..how can create that?
I should create an action in the controller class????


The new $model->search() was introduced in Yii 1.1.1 release. You can download the latest Yii 1.1.2, and look into Blog demo code for reference. Or you can check out the live demo at http://www.yiiframew...com/demos/blog/, the search and filter feature of CGridView can be found at "Manage Post" after logged in as demo user account.

-Jason
0

#4 User is offline   kitty10 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 139
  • Joined: 13-April 10

Posted 05 May 2010 - 08:27 AM

View Postjaxyeh, on 05 May 2010 - 06:53 AM, said:

The new $model->search() was introduced in Yii 1.1.1 release. You can download the latest Yii 1.1.2, and look into Blog demo code for reference. Or you can check out the live demo at http://www.yiiframew...com/demos/blog/, the search and filter feature of CGridView can be found at "Manage Post" after logged in as demo user account.

-Jason


Yes but i want the code to implement in my application the search() method
-3

#5 User is offline   zitter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 140
  • Joined: 14-July 09

Posted 05 May 2010 - 12:18 PM

@kitty: you HAVE the code, you need only copy/paste. Code is in yii/demos/blog/ direcotry in downloaded Yii...
0

#6 User is offline   YIINewBy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 11-September 09
  • Location:Pretoria, South Africa

Posted 08 May 2010 - 04:02 AM

View Postjaxyeh, on 04 May 2010 - 12:58 PM, said:

Yii Community,

I have been searching all over the Internet, but still couldn't figure out how to setup search and filter capabilities for a relationship field through CGridView... Here is example of data structure:

Here is snippet example of User Model:
	public function rules()
	{
		return array(
			array('name', 'required'),
			array('org_id', 'numerical', 'integerOnly'=>true),
			array('created_date', 'type', 'type'=>'date'),
			array('created_date, org.org_name', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('name, org.org_name, created_date', 'safe', 'on'=>'search'),
		);
	}
	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(
			'org' => array(self::BELONGS_TO, 'Organization', 'org_id'),
		);
	}
	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('org.org_name',$this->org->org_name,true); // That didn't work

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

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


Here is example of User Admin View:
<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'calendar-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
	array( 
			'name'=>'name',
			'type'=>'raw',
			'value'=>'CHtml::link(CHtml::encode($data->name), array("event/view", "id"=>$data->id))',
		),
		array(
			'name'=>'org.org_name', //this lookup works
    // I need solution here to do filter & search of this relationship data field
		),
		array(
            'name'=>'created_date',
            //'type'=>'date',
            'filter'=>false,
        ),
		array(
			'class'=>'CButtonColumn',
		),
	),
)); ?>



I had searched this topic over this forum, found similar discussion without direct solution:


Hopefully someone can help me to pinpoint the RIGHT solution to setup AJAX search & filter of a relation field through CGridView.

Thanks,
Jason




Hi Jason

I had a similar problem I however solved this by appending t infront of the column names in the compare statement. Yii uses t by default.

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

		$criteria=new CDbCriteria;

		$criteria->compare('t.id',$this->id);
		
		$criteria->compare('org.org_name',$this->org_id,true); 

		$criteria->compare('t.name',$this->name,true);

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


Try the above


Hope it helps!


http://www.techportal.co.za
0

#7 User is offline   leandrofranca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 19-January 09
  • Location:Salvador, Bahia, Brasil

Posted 08 May 2010 - 11:45 PM

View PostYIINewBy, on 08 May 2010 - 04:02 AM, said:

Hi Jason

I had a similar problem I however solved this by appending t infront of the column names in the compare statement. Yii uses t by default.

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

		$criteria=new CDbCriteria;

		$criteria->compare('t.id',$this->id);
		
		$criteria->compare('org.org_name',$this->org_id,true); 

		$criteria->compare('t.name',$this->name,true);

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


Try the above


Hope it helps!


http://www.techportal.co.za


This will work to id and name but org.org_name will don't work.
0

#8 User is offline   YIINewBy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 11-September 09
  • Location:Pretoria, South Africa

Posted 09 May 2010 - 05:30 AM

View Postleandrofranca, on 08 May 2010 - 11:45 PM, said:

This will work to id and name but org.org_name will don't work.



By adding this line could maybe work $criteria->with='org';
0

#9 User is offline   eval 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 85
  • Joined: 25-November 08
  • Location:Athens - Greece

Posted 09 May 2010 - 10:44 AM

*
POPULAR

As a data column for each relation give the following (the example shows user's country from the country_id in user table and a dropdown filter to select users from each country):
array(
            'name' => 'country_id',
            'filter' => CHtml::listData(Country::model()->findAll(), 'country_id', 'country_name'), // fields from country table
            'value' => 'Country::Model()->FindByPk($data->country_id)->country',
        ),


That should work and generate you a dropdown filter for filtering based on country.

HTH
8

#10 User is offline   leandrofranca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 19-January 09
  • Location:Salvador, Bahia, Brasil

Posted 09 May 2010 - 10:26 PM

View Posteval, on 09 May 2010 - 10:44 AM, said:

As a data column for each relation give the following (the example shows user's country from the country_id in user table and a dropdown filter to select users from each country):
array(
            'name' => 'country_id',
            'filter' => CHtml::listData(Country::model()->findAll(), 'country_id', 'country_name'), // fields from country table
            'value' => 'Country::Model()->FindByPk($data->country_id)->country',
        ),


That should work and generate you a dropdown filter for filtering based on country.

HTH



Yeeepp it's working!

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'pacote-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'emptyText'=>param('noResult'),
	'summaryText'=>'Total: {count}',
	'pagerCssClass'=>'alignCenter',
	'columns'=>array(
		'nome',
		array(
			'name'=>'roteiroId',
			'filter'=>CHtml::listData(Roteiro::model()->findAll(), 'id', 'descricao'),
			'value'=>'Roteiro::Model()->FindByPk($data->roteiroId)->descricao',
		),
		array(
			'name'=>'tipopacoteId',
			'filter'=>CHtml::listData(TipoPacote::model()->findAll(), 'id', 'descricao'),
			'value'=>'TipoPacote::Model()->FindByPk($data->tipopacoteId)->descricao',
		),
		array(
			'name'=>'destaque',
			'filter'=>CHtml::activeDropDownList($model, 'destaque', Pacote::model()->statusOptions),
			'value'=>'Pacote::model()->statusOptions[$data->destaque]',
		),
	),
)); ?>

0

#11 User is offline   jaxyeh 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 23-February 10

Posted 10 May 2010 - 11:48 AM

It solved my problem too! Thank you everyone in this forum for getting answer together!

Cheers,
Jason
0

#12 User is offline   echo66 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 66
  • Joined: 06-May 10

Posted 05 June 2010 - 07:17 PM

hi!

I'm having a similar problem here...

here is my model code for CommunityUser

<?php

class CommunityUser extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'community_user':
	 * @var integer $user_id
	 * @var integer $community_id
	 * @var integer $is_suspended
	 * @var integer $can_invite_users
	 * @var integer $can_manage_objects
	 * @var integer $can_manage_templates
	 * @var integer $can_deactivate_community
	 * @var integer $can_manage_members
	 * @var integer $can_close_community
	 * @var integer $can_manage_requests
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CommunityUser the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'community_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, community_id', 'required'),
			array('user_id, community_id, is_suspended, can_invite_users, can_manage_objects, can_manage_templates, can_deactivate_community, can_manage_members, can_close_community, can_manage_requests', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('user.username,user.name,user.photo,user_id, community_id, is_suspended, can_invite_users, can_manage_objects, can_manage_templates, can_deactivate_community, can_manage_members, can_close_community, can_manage_requests', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @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(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'community' => array(self::BELONGS_TO, 'Community', 'community_id'),
			
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User',
			'community_id' => 'Community',
			'is_suspended' => 'Is Suspended',
			'can_invite_users' => 'Can Invite Users',
			'can_manage_objects' => 'Can Manage Objects',
			'can_manage_templates' => 'Can Manage Templates',
			'can_deactivate_community' => 'Can Deactivate Community',
			'can_manage_members' => 'Can Manage Members',
			'can_close_community' => 'Can Close Community',
			'can_manage_requests' => 'Can Manage Requests',
		);
	}
	
	
	public function setDefaultsToNull(){
		$this->is_suspended = null;
	 	$this->can_invite_users = null;
	 	$this->can_manage_objects = null;
	 	$this->can_manage_templates = null;
	 	$this->can_deactivate_community = null;
	 	$this->can_manage_members = null;
	 	$this->can_close_community = null;
	 	$this->can_manage_requests = null;
	}

	/**
	 * 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.

		$criteria=new CDbCriteria;

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

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

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

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

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

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

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

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

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

		$criteria->compare('can_manage_requests',$this->can_manage_requests);
		
		$criteria->compare('user.username',$this->user_id);
				
		$criteria->compare('user.name',$this->user_id);
				
		$criteria->compare('user.photo',$this->user_id);
		
		$criteria->with = 'user';

		return new CActiveDataProvider('CommunityUser', array(
			'criteria'=>$criteria,
		));
	}
	
	
	public function searchPerCommunity($communityId)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		
		//$criteria->condition = 'community_user.community_id = '. $community_id;
		
		$criteria->join = 'JOIN user as t2 ON t2.id = t.user_id';

		$criteria->compare('t.user_id',$this->user_id);

		$criteria->compare('t.community_id',$communityId);

		$criteria->compare('t.is_suspended',$this->is_suspended);

		$criteria->compare('t.can_invite_users',$this->can_invite_users);

		$criteria->compare('t.can_manage_objects',$this->can_manage_objects);

		$criteria->compare('t.can_manage_templates',$this->can_manage_templates);

		$criteria->compare('t.can_deactivate_community',$this->can_deactivate_community);

		$criteria->compare('t.can_manage_members',$this->can_manage_members);

		$criteria->compare('t.can_close_community',$this->can_close_community);

		$criteria->compare('t.can_manage_requests',$this->can_manage_requests);
		
		$criteria->compare('user.username',$this->user_id);
		
		$criteria->compare('user.name',$this->user_id);
		
		$criteria->compare('user.photo',$this->user_id);
		
		$criteria->with = 'user';

		return new CActiveDataProvider('CommunityUser', array(
			'criteria'=>$criteria,
		));
	}
	
	
	
	
	public function temp($communityId)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('t.community_id',$communityId);
		
		$criteria->compare('user.username',$this->user_id);
//		
//		$criteria->compare('user.name',$this->user_id);
//		
//		$criteria->compare('user.photo',$this->user_id);
		
		$criteria->with = 'user';

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




my view source:

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'grid',
	'dataProvider'=>$dataProvider->search(),
	'filter'=>$dataProvider,
	'columns'=>array(
		'user_id',
		'user.username',
array( 'class'=>'CLinkColumn',
								'label'=>"modificar <br/>permissões",
		                        'urlExpression'=>'Yii::app()->createUrl("/community/userrights",array("user_id"=>$data->user_id, "community_id"=>$data->community_id))',
		                        'linkHtmlOptions'=>array('title'=>'Modificar permissões do membro'),
				),
				array( 'class'=>'CLinkColumn',
									'label'=>"remover",
			                        'urlExpression'=>'Yii::app()->createUrl("/community/removeuser",array("user_id"=>$data->user_id, "community_id"=>$data->community_id))',
			                        'linkHtmlOptions'=>array('title'=>'Remover o membro.'),
				),
				array( 'class'=>'CLinkColumn',
								'label'=>"activar",
		                        'urlExpression'=>'Yii::app()->createUrl("/community/activateuser",array("user_id"=>$data->user_id, "community_id"=>$data->community_id))',
		                        'linkHtmlOptions'=>array('title'=>'Activar o membro.'),
				),
				array( 'class'=>'CLinkColumn',
								'label'=>"suspender",
		                        'urlExpression'=>'Yii::app()->createUrl("/community/suspenduser",array("user_id"=>$data->user_id, "community_id"=>$data->community_id))',
		                        'linkHtmlOptions'=>array('title'=>'Suspender o membro.'),
				),
	),
	)
	);?>


The problem is that i the cgridview doesn't generate a box to filter the username field!! I tried the way you guys described but no success...heeeeelp :unsure:
0

#13 User is offline   echo66 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 66
  • Joined: 06-May 10

Posted 06 June 2010 - 11:41 AM

anyone? :(
0

#14 User is offline   leandrofranca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 19-January 09
  • Location:Salvador, Bahia, Brasil

Posted 06 June 2010 - 01:16 PM

View Postecho66, on 06 June 2010 - 11:41 AM, said:

anyone? :(


Remember that you need to specify the field username as a listData:
'columns'=>array(
                array(
                        'name'=>'username',
                        'filter'=>CHtml::listData(User::model()->findAll(), 'id', 'name'),
                        'value'=>'User::Model()->FindByPk($data->username)->name',
                ),
...

0

#15 User is offline   echo66 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 66
  • Joined: 06-May 10

Posted 06 June 2010 - 03:31 PM

thanks for the reply.

before continuing, it's better if i tell you the schema of the tables user and community_user.


CREATE TABLE user(
	id INT NOT NULL AUTO_INCREMENT,
	clip_id INT,
	username VARCHAR(45) NOT NULL,
	password CHAR(40) NOT NULL, -- sha1 ?
	name VARCHAR(100),
	photo VARCHAR(100),
	location POINT,
	birthday DATE,
	sex ENUM('F','M'),
	
	INDEX (id),
	PRIMARY KEY (id)
) ENGINE = InnoDB;


CREATE TABLE community_user(
	user_id INT NOT NULL,
	community_id INT NOT NULL,
	is_suspended BOOLEAN DEFAULT FALSE,
	can_invite_users BOOLEAN DEFAULT TRUE,
	can_manage_objects BOOLEAN DEFAULT TRUE,
	can_manage_templates BOOLEAN DEFAULT FALSE,
	can_deactivate_community BOOLEAN DEFAULT FALSE,
	can_manage_members BOOLEAN DEFAULT FALSE,
	can_close_community BOOLEAN DEFAULT FALSE,
	can_manage_requests BOOLEAN DEFAULT FALSE,
	
	PRIMARY KEY (user_id, community_id)
	
) ENGINE = InnoDB;



this is the action in the controller:

public function actionMainIndex(){
		
		if(isset($_GET['community_id'])){
				$community = Community::model()->findByPk($_GET['community_id']);
				
				if($community == null){
					$message = 'Comunidade inexistente';
					$actions = null;
					$this->render('/site/errorpage',array('message'=>$message,'actions'=>$actions));
					return;
				}
				
				$this->layout='application.views.layouts.main';

				$dataProvider = new CommunityUser('search');
				$dataProvider->setDefaultsToNull();
				
				if(isset($_GET['CommunityUser'])){
					$dataProvider->attributes = $_GET['CommunityUser'];
				}
				
				$failureMessage = null;
				$successMessage = null;
				
				if(isset($_GET['failuremessage']))
					$failureMessage = $_GET['failuremessage'];
				if(isset($_GET['successmessage']))
					$successMessage = $_GET['successmessage'];
					
				$isMember = CommunityController::isCurrentUserMember($community->id);
							
				$this->render('/community/mainindex', array('dataProvider'=>$dataProvider,'community'=>$community, 'isMember'=>$isMember, 'successMessage'=>$successMessage, 'failureMessage'=>$failureMessage));
				return;
		}else{
			$message = 'Parâmetros incorrectos';
			$actions = null;
			$this->render('/site/errorpage',array('message'=>$message,'actions'=>$actions));
			return;
		}
	}



and now, the view:

$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'grid',
	'dataProvider'=>$dataProvider->search(),
	'filter'=>$dataProvider,
	'columns'=>array(
		
		'user_id',
		array(
			'header'=>'Username',
			'name'=>'user.username',
			'filter'=>CHtml::listData(User::model()->findAll(), 'id', 'username'),
			'value'=>'$data->user->username',
		),
		array(
			'name'=>'is_suspended',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("is_suspended")',
		),
		array(
			'name'=>'can_manage_objects',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_manage_objects")',
		),
		array(
			'name'=>'can_manage_templates',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_manage_templates")',
		),
		array(
			'name'=>'can_deactivate_community',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_deactivate_community")',
		),
		array(
			'name'=>'can_manage_members',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_manage_members")',
		),
		array(
			'name'=>'can_close_community',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_close_community")',
		),
		array(
			'name'=>'can_manage_requests',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_manage_requests")',
		),
		
		
		array( 'class'=>'CButtonColumn',
			'template'=>'{modifyPermissions} {removeUser} {activateUser} {suspendUser}',
			'buttons'=>array(
				'modifyPermissions' => array(
							    'label'=>'Modificar permissões do membro',     // text label of the button
							    'url'=>'Yii::app()->createUrl("/community/userrights",array("user_id"=>$data->user_id, "community_id"=>$data->community_id))',       // a PHP expression for generating the URL of the button
							    'imageUrl'=>'images/buttons/16x16/gif/69.gif',  // image URL of the button. If not set or false, a text link is used
							    'htmlOptions'=>array('title'=>'modificar permissões'), // HTML options for the button tag
							    'visible'=>'1',   // a PHP expression for determining whether the button is visible
							),
				'removeUser' => array(
								'label'=>'Remover membro',     // text label of the button
							    'url'=>'Yii::app()->createUrl("/community/removeuser",array("user_id"=>$data->user_id, "community_id"=>$data->community_id))',       // a PHP expression for generating the URL of the button
							    'imageUrl'=>'images/buttons/16x16/gif/33.gif',  // image URL of the button. If not set or false, a text link is used
							    'htmlOptions'=>array('title'=>'remover membro'), // HTML options for the button tag
							    'visible'=> '!$data->can_manage_members',   // a PHP expression for determining whether the button is visible
							),
				'activateUser' => array(
								'label'=>'Activar membro',     // text label of the button
							    'url'=>'Yii::app()->createUrl("/community/activateuser",array("user_id"=>$data->user_id, "community_id"=>$data->community_id))',       // a PHP expression for generating the URL of the button
							    'imageUrl'=>'images/buttons/16x16/gif/3.gif',  // image URL of the button. If not set or false, a text link is used
							    'htmlOptions'=>array('title'=>'activar membro'), // HTML options for the button tag
							    'visible'=>'$data->is_suspended',   // a PHP expression for determining whether the button is visible
							),
				'suspendUser' => array(
								'label'=>'Suspender membro',     // text label of the button
							    'url'=>'Yii::app()->createUrl("/community/suspenduser",array("user_id"=>$data->user_id, "community_id"=>$data->community_id))',       // a PHP expression for generating the URL of the button
							    'imageUrl'=>'images/buttons/16x16/gif/4.gif',  // image URL of the button. If not set or false, a text link is used
							    'visible'=>'!$data->is_suspended',   // a PHP expression for determining whether the button is visible
							),
				
			)
		),
	),
	)
	);?>


The problem is that this solution still leaves any chance of filter out of question....i tried and no success. The only way to put a filter there is by using a field of the model CommunityUser...and that isn't my objective :unsure:

Help needed..
0

#16 User is offline   leandrofranca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 19-January 09
  • Location:Salvador, Bahia, Brasil

Posted 06 June 2010 - 06:34 PM

View Postecho66, on 06 June 2010 - 03:31 PM, said:

thanks for the reply.

before continuing, it's better if i tell you the schema of the tables user and community_user.


CREATE TABLE user(
	id INT NOT NULL AUTO_INCREMENT,
	clip_id INT,
	username VARCHAR(45) NOT NULL,
	password CHAR(40) NOT NULL, -- sha1 ?
	name VARCHAR(100),
	photo VARCHAR(100),
	location POINT,
	birthday DATE,
	sex ENUM('F','M'),
	
	INDEX (id),
	PRIMARY KEY (id)
) ENGINE = InnoDB;


CREATE TABLE community_user(
	user_id INT NOT NULL,
	community_id INT NOT NULL,
	is_suspended BOOLEAN DEFAULT FALSE,
	can_invite_users BOOLEAN DEFAULT TRUE,
	can_manage_objects BOOLEAN DEFAULT TRUE,
	can_manage_templates BOOLEAN DEFAULT FALSE,
	can_deactivate_community BOOLEAN DEFAULT FALSE,
	can_manage_members BOOLEAN DEFAULT FALSE,
	can_close_community BOOLEAN DEFAULT FALSE,
	can_manage_requests BOOLEAN DEFAULT FALSE,
	
	PRIMARY KEY (user_id, community_id)
	
) ENGINE = InnoDB;



this is the action in the controller:

public function actionMainIndex(){
		
		if(isset($_GET['community_id'])){
				$community = Community::model()->findByPk($_GET['community_id']);
				
				if($community == null){
					$message = 'Comunidade inexistente';
					$actions = null;
					$this->render('/site/errorpage',array('message'=>$message,'actions'=>$actions));
					return;
				}
				
				$this->layout='application.views.layouts.main';

				$dataProvider = new CommunityUser('search');
				$dataProvider->setDefaultsToNull();
				
				if(isset($_GET['CommunityUser'])){
					$dataProvider->attributes = $_GET['CommunityUser'];
				}
				
				$failureMessage = null;
				$successMessage = null;
				
				if(isset($_GET['failuremessage']))
					$failureMessage = $_GET['failuremessage'];
				if(isset($_GET['successmessage']))
					$successMessage = $_GET['successmessage'];
					
				$isMember = CommunityController::isCurrentUserMember($community->id);
							
				$this->render('/community/mainindex', array('dataProvider'=>$dataProvider,'community'=>$community, 'isMember'=>$isMember, 'successMessage'=>$successMessage, 'failureMessage'=>$failureMessage));
				return;
		}else{
			$message = 'Parâmetros incorrectos';
			$actions = null;
			$this->render('/site/errorpage',array('message'=>$message,'actions'=>$actions));
			return;
		}
	}



and now, the view:

$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'grid',
	'dataProvider'=>$dataProvider->search(),
	'filter'=>$dataProvider,
	'columns'=>array(
		
		'user_id',
		array(
			'header'=>'Username',
			'name'=>'user.username',
			'filter'=>CHtml::listData(User::model()->findAll(), 'id', 'username'),
			'value'=>'$data->user->username',
		),
		array(
			'name'=>'is_suspended',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("is_suspended")',
		),
		array(
			'name'=>'can_manage_objects',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_manage_objects")',
		),
		array(
			'name'=>'can_manage_templates',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_manage_templates")',
		),
		array(
			'name'=>'can_deactivate_community',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_deactivate_community")',
		),
		array(
			'name'=>'can_manage_members',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_manage_members")',
		),
		array(
			'name'=>'can_close_community',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_close_community")',
		),
		array(
			'name'=>'can_manage_requests',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_manage_requests")',
		),
		
		
		array( 'class'=>'CButtonColumn',
			'template'=>'{modifyPermissions} {removeUser} {activateUser} {suspendUser}',
			'buttons'=>array(
				'modifyPermissions' => array(
							    'label'=>'Modificar permissões do membro',     // text label of the button
							    'url'=>'Yii::app()->createUrl("/community/userrights",array("user_id"=>$data->user_id, "community_id"=>$data->community_id))',       // a PHP expression for generating the URL of the button
							    'imageUrl'=>'images/buttons/16x16/gif/69.gif',  // image URL of the button. If not set or false, a text link is used
							    'htmlOptions'=>array('title'=>'modificar permissões'), // HTML options for the button tag
							    'visible'=>'1',   // a PHP expression for determining whether the button is visible
							),
				'removeUser' => array(
								'label'=>'Remover membro',     // text label of the button
							    'url'=>'Yii::app()->createUrl("/community/removeuser",array("user_id"=>$data->user_id, "community_id"=>$data->community_id))',       // a PHP expression for generating the URL of the button
							    'imageUrl'=>'images/buttons/16x16/gif/33.gif',  // image URL of the button. If not set or false, a text link is used
							    'htmlOptions'=>array('title'=>'remover membro'), // HTML options for the button tag
							    'visible'=> '!$data->can_manage_members',   // a PHP expression for determining whether the button is visible
							),
				'activateUser' => array(
								'label'=>'Activar membro',     // text label of the button
							    'url'=>'Yii::app()->createUrl("/community/activateuser",array("user_id"=>$data->user_id, "community_id"=>$data->community_id))',       // a PHP expression for generating the URL of the button
							    'imageUrl'=>'images/buttons/16x16/gif/3.gif',  // image URL of the button. If not set or false, a text link is used
							    'htmlOptions'=>array('title'=>'activar membro'), // HTML options for the button tag
							    'visible'=>'$data->is_suspended',   // a PHP expression for determining whether the button is visible
							),
				'suspendUser' => array(
								'label'=>'Suspender membro',     // text label of the button
							    'url'=>'Yii::app()->createUrl("/community/suspenduser",array("user_id"=>$data->user_id, "community_id"=>$data->community_id))',       // a PHP expression for generating the URL of the button
							    'imageUrl'=>'images/buttons/16x16/gif/4.gif',  // image URL of the button. If not set or false, a text link is used
							    'visible'=>'!$data->is_suspended',   // a PHP expression for determining whether the button is visible
							),
				
			)
		),
	),
	)
	);?>


The problem is that this solution still leaves any chance of filter out of question....i tried and no success. The only way to put a filter there is by using a field of the model CommunityUser...and that isn't my objective :unsure:

Help needed..


Before anything. Do you speak Portuguese?
If you need to display the attribute in view then add this attribute in Model as a public attribute.
0

#17 User is offline   echo66 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 66
  • Joined: 06-May 10

Posted 06 June 2010 - 07:32 PM

sim, falo português. :D

now, i added the username attribute as public in User model

<?php

class User extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'user':
	 * @var integer $id
	 * @var integer $clip_id
	 * @var string $username
	 * @var string $password
	 * @var string $name
	 * @var string $photo
	 * @var integer $location
	 * @var string $birthday
	 * @var string $sex
	 */
	public $username;

....





and the view:

<?php 


$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'grid',
	'dataProvider'=>$dataProvider->search(),
	'filter'=>$dataProvider,
	'columns'=>array(
		
		'user_id',
		array(
			'header'=>'Username',
			'name'=>'user.username',
			'filter'=>CHtml::listData(User::model()->findAll(), 'id', 'username'),
			'value'=>'$data->user->username',
		),
		array(
			'name'=>'is_suspended',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("is_suspended")',
		),
		array(
			'name'=>'can_manage_objects',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_manage_objects")',
		),
		array(
			'name'=>'can_manage_templates',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_manage_templates")',
		),
		array(
			'name'=>'can_deactivate_community',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_deactivate_community")',
		),
		array(
			'name'=>'can_manage_members',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_manage_members")',
		),
		array(
			'name'=>'can_close_community',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_close_community")',
		),
		array(
			'name'=>'can_manage_requests',
			'filter'=>array(0=>'Não', 1=> 'Sim'),
			'value'=>'$data->getYesNo("can_manage_requests")',
		),
		
		
		array( 'class'=>'CButtonColumn',
			'template'=>'{modifyPermissions} {removeUser} {activateUser} {suspendUser}',
			'buttons'=>array(
				'modifyPermissions' => array(
							    'label'=>'Modificar permissões do membro',     // text label of the button
							    'url'=>'Yii::app()->createUrl("/community/userrights",array("user_id"=>$data->user_id, "community_id"=>$data->community_id))',       // a PHP expression for generating the URL of the button
							    'imageUrl'=>'images/buttons/16x16/gif/69.gif',  // image URL of the button. If not set or false, a text link is used
							    'htmlOptions'=>array('title'=>'modificar permissões'), // HTML options for the button tag
							    'visible'=>'1',   // a PHP expression for determining whether the button is visible
							),
				'removeUser' => array(
								'label'=>'Remover membro',     // text label of the button
							    'url'=>'Yii::app()->createUrl("/community/removeuser",array("user_id"=>$data->user_id, "community_id"=>$data->community_id))',       // a PHP expression for generating the URL of the button
							    'imageUrl'=>'images/buttons/16x16/gif/33.gif',  // image URL of the button. If not set or false, a text link is used
							    'htmlOptions'=>array('title'=>'remover membro'), // HTML options for the button tag
							    'visible'=> '!$data->can_manage_members',   // a PHP expression for determining whether the button is visible
							),
				'activateUser' => array(
								'label'=>'Activar membro',     // text label of the button
							    'url'=>'Yii::app()->createUrl("/community/activateuser",array("user_id"=>$data->user_id, "community_id"=>$data->community_id))',       // a PHP expression for generating the URL of the button
							    'imageUrl'=>'images/buttons/16x16/gif/3.gif',  // image URL of the button. If not set or false, a text link is used
							    'htmlOptions'=>array('title'=>'activar membro'), // HTML options for the button tag
							    'visible'=>'$data->is_suspended',   // a PHP expression for determining whether the button is visible
							),
				'suspendUser' => array(
								'label'=>'Suspender membro',     // text label of the button
							    'url'=>'Yii::app()->createUrl("/community/suspenduser",array("user_id"=>$data->user_id, "community_id"=>$data->community_id))',       // a PHP expression for generating the URL of the button
							    'imageUrl'=>'images/buttons/16x16/gif/4.gif',  // image URL of the button. If not set or false, a text link is used
							    'visible'=>'!$data->is_suspended',   // a PHP expression for determining whether the button is visible
							),
				
			)
		),
	),
	)
	);?>


what did i do wrong???

CGridView is really giving me some serious problems.... <_<
0

#18 User is offline   leandrofranca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 19-January 09
  • Location:Salvador, Bahia, Brasil

Posted 06 June 2010 - 07:49 PM

View Postecho66, on 06 June 2010 - 07:32 PM, said:

sim, falo português. :D

now, i added the username attribute as public in User model


Sorry I'm speaking bullshit...
You don't need the public attribute username in your Model because you have the relation "user".
What you need to is...

In Model (search) method:
$criteria->compare('t.user_id',$this->user_id);


And in View...
		array(
			'name'=>'user_id',
			'filter'=>CHtml::listData(User::model()->findAll(), 'id', 'name'),
			'value'=>'User::Model()->FindByPk($data->user_id)->name',
		),


Try it!
1

#19 User is offline   echo66 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 66
  • Joined: 06-May 10

Posted 06 June 2010 - 09:47 PM

i finally did it! Thanks!!!!! ;D


Another thing...instead of having a dropdown menu, how can i have a text field like the others?
0

#20 User is offline   leandrofranca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 19-January 09
  • Location:Salvador, Bahia, Brasil

Posted 06 June 2010 - 09:54 PM

View Postecho66, on 06 June 2010 - 09:47 PM, said:

i finally did it! Thanks!!!!! ;D


Another thing...instead of having a dropdown menu, how can i have a text field like the others?


if in compare method you do
   $criteria->compare('user.name',$this->user->name);


and don't work then you will need a method in your model to parse from "text" to "id" then search like a combobox! :)
0

Share this topic:


  • (3 Pages)
  • +
  • 1
  • 2
  • 3
  • 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