Answer the question
In order to leave comments, you need to log in
How to filter and sort data from adjacent tables in Yii1?
There is Yii1. It has 2 models Users(id,name,surename) and Contacts(id,phone,email). The relationship between models is 1 to 1.
In the users model, the relationship is described as follows:
public function relations()
{
return array(
'contacts'=>array(self::HAS_ONE, 'contacts', 'id'),
);
}
'phone'=>array(
'name'=>'User phone',
'value'=>'$data->contacts->phone',
),
'phone'=>array(
'name'=>'User phone',
'value'=>'$data->contacts->phone',
'filter'=>true,
),
Answer the question
In order to leave comments, you need to log in
The first option did not work because when rendering the filter, it tried to access the "User phone" property of the Users model, as you understand, there is no such property.
In the second case, it doesn't refer to St. because you explicitly set the filter value to it, by the way, it's wrong - there should be html code there.
In this case, it is better to act like this:
1. In the Users model, add the property (namely, the property of the class and not the field in the database) phone,
class Users extends CActiveRecord {
public $phone;
...
}
[
'header'=>'Здесь заголовок колонки',
'value'=>function($data){ return $data->contacts->phone;} // или просто '$data->contacts->phone',
'filter'=>CHtml::activeTextField($model,'phone')
],
if ($this->phone){
$criteria->with[] = 'contacts';
$criteria->compare('contacts.phone', $this->phone) // третьим параметром добавьте true если будете искать неполное совпадение
}
$model = new Users('search');
if (array_key_exists('Users', $_GET)){
$model->attributes = $_GET['Users'];
}
[
'name'=>'phone',
'value'=>function($data){ return $data->contacts->phone;} // или просто '$data->contacts->phone',
],
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question