Hi.
I’m trying to feed a CGridView with data, using a CActiveDataProvider. Here is my code:
<?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_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',
);
}
/**
* 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->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,
));
}
public function getYesNo($field){
$val = $this->getAttribute($field);
if($val == 1)
return "Sim";
else
return "Não";
}
}
my view
<?php
if($isMember){
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$dataProvider->search(),
'filter'=>$dataProvider,
'columns'=>array(
array(
'name'=>'user_id',
'value'=>'$data->user_id',
),
),
));
}
?>
The problem is that the CActiveDataProvider i’m using isn’t returning any records at all!!! The only way i can get the records is by comenting all these lines:
// $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);
but…what the hell? why is this happening?
Thanks