D
D
dro1d2016-09-06 14:15:54
Yii
dro1d, 2016-09-06 14:15:54

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'),
    );
  }

In the admin view of users in the standard widget zii.widgets.grid.CGridView I add the following element to 'columns':
'phone'=>array( 
      'name'=>'User phone',
      'value'=>'$data->contacts->phone',
    ),

As a result, I get the following error:
73ca0bbe58af41bb8facdc789e0e883a.png
If I add one line:
'phone'=>array( 
      'name'=>'User phone',
      'value'=>'$data->contacts->phone',
      'filter'=>true,	
    ),

We get:
1e4fef55d3154017b5d38d9a1401f612.png
How to make full filtering and search available for these fields?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cage, 2016-09-07
@dro1d

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;
  ...
}

2. Write it in the rules() method of the Users class
3. in the widget settings, use the following construction for this column
[
  'header'=>'Здесь заголовок колонки',
  'value'=>function($data){ return $data->contacts->phone;} // или просто '$data->contacts->phone',
  'filter'=>CHtml::activeTextField($model,'phone')
],

4. In the search method of the Users class, add the following code
if ($this->phone){
  $criteria->with[] = 'contacts';
  $criteria->compare('contacts.phone', $this->phone) // третьим параметром добавьте true если будете искать неполное совпадение
}

Now the explanation of all this crap
1. The property in the model is necessary to store the value when the search will be used.
2. Adding a property to the rules method is necessary for the safe assignment of the attribute, this is when in an action you will do this
$model = new Users('search');
if (array_key_exists('Users', $_GET)){
  $model->attributes = $_GET['Users'];
}

3. The widget is actually configured very flexibly there can be many options. I wrote one of them as the simplest one, to understand how the filter is rendered, you can look at the CDataColumn::getFilterCellContent() method, for example, you could use the following construction:
[
  'name'=>'phone',
  'value'=>function($data){ return $data->contacts->phone;} // или просто '$data->contacts->phone',
],

4. Actually, this is the implementation of a search by phone number. There may be other implementation options, but this one is the most correct and convenient.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question