Hi,
Actually this question has been asked several times before. But unfortunately I still could not achieve the expected result even after tried so hard. So I am posting this here because someone can identify where I got wrong.
Basically I have 2 tables user and address. In user table I store user’s first name, last name etc. and in address table company name, address, city etc… The relationship between 2 tables shows below.
And the view is given below.
In the above view company field cannot be sorted. I want it to be sorted.
model of the user table is given below.
class User extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return User the static model class
*/
public $confirm_email;
public $confirm_password;
public $new_password;
public $company;
public $agree=0;
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return '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('title, first_name, last_name,
array('how_did_you_hear, describe_business, estimated_purchase, client_before, confirm_email, address, company, user_id', 'safe'),
);
}
/**
* @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(
'address' => array(self::HAS_ONE, 'Address', 'user_id'),
'authassignment' => array(self::HAS_ONE, 'Authassignment', 'userid'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'customer_id' => 'Customer',
'title' => 'Title',
'first_name' => 'First Name',
'last_name' => 'Last Name',
// 'user_name' => 'User Name',
'password' => 'Password',
'confirm_password' => 'Confirm Password',
'email' => 'Email',
'date_added' => 'Date Added',
'date_updated' => 'Date Updated',
'status' => 'Status',
);
}
public function search_customer()
{
$criteria=new CDbCriteria;
$criteria->compare('title',$this->title,true);
$criteria->compare('first_name',$this->first_name,true);
$criteria->compare('last_name',$this->last_name,true);
$criteria->compare('user_name',$this->user_name,true);
$criteria->compare('password',$this->password,true);
$criteria->compare('email',$this->email,true);
$criteria->compare('date_added',$this->date_added);
$criteria->compare('date_updated',$this->date_updated);
$criteria->compare('status',$this->status);
$criteria->together = true;
$criteria->condition = ('authassignment.itemname="xxxxx"');
$criteria->with = array('address', 'authassignment');
$criteria->compare('address.company', $this->company, true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=> array(
'attributes' => array(
'company' => array(
'asc' => 'address.company ASC',
'desc' => 'address.company DESC'
),
'*',
),
)
));
}
In above code I only added necessary parts of my actual User model. Of that note following point
-
Add $company as a public property in User.php
-
Add company under rules in ‘safe’ section
-
Add $criteria->with = array(‘address’, ‘authassignment’); in customer search
Below is UserController.php Only necessary part.
/**
* Manages all models.
*/
public function actionAdmin()
{
$model=new User('search_customer');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['User']))
$model->attributes=$_GET['User'];
$this->render('admin',array(
'model'=>$model,
));
}
and admin.php(view)
<?php $this->widget('bootstrap.widgets.TbGridView', array(
'id'=>'user-grid',
'type' => array('striped', 'bordered', 'condensed','hover'),
'dataProvider'=>$model->search_customer(),
'filter'=>$model,
'columns'=>array(
array(
'id'=>'user_id',
'class'=>'CCheckBoxColumn',
'selectableRows' => '50',
'selectableRows'=>2,
'value'=>$model->user_id
),
'email',
'first_name',
'last_name',
array(
'name' => 'address.company',
'filter'=>CHtml::activeTextField($model,'company'),
),
// 'address.company',
'address.postcode',
'address.telephone',
'address.mobile',
'authassignment.itemname',
//'user_name',
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
'template' => '{view} {update} {delete}',
),
),
)); ?>
And the links which I got instructions given below.
http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/
http://www.yiiframework.com/forum/index.php/topic/20442-model-search-criteria-on-related-record/
If any one can help on this matter it would be very grateful.