Simple extension for making searching and filtering by related field simple.
BSD-3-CLAUSE License
Simple extension for making searching and filtering by related field simple.
Use this trait to easily add ability to display/filter by related entity attribute. It also allows to use table aliases. Extension is pretty simple but makes the code cleaner, especially when using aliases (there are some auto-formatting bugs in some IDEs).
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist codiverum/yii2-relation-search-filter "*"
or add
"codiverum/yii2-relation-search-filter": "*"
to the require section of your composer.json
file.
To use the trait you need to do the following (first example without using aliases):
use \codiverum\relationSF\RelationSFTrait;
public $relation_name;
$this->joinWithRelation($query, 'relation_name');
$this->addRelationSort($dataProvider, 'relation_name', 'related_table_field_name', 'table_name');
$this->addRelationFilter($query, 'relation_name', 'related_table_field_name', 'table_name');
[
'attribute' => 'relation_name',
'value' => 'relation_name.related_table_field_name',
],
That's it.
use \codiverum\relationSF\RelationSFTrait;
public $relation_name;
$this->joinWithRelation($query, 'relation_name', 'table_name', 'alias');
$this->addRelationSort($dataProvider, 'relation_name', 'related_table_field_name', 'alias');
$this->addRelationFilter($query, 'relation_name', 'related_table_field_name', 'alias');
[
'attribute' => 'relation_name',
'value' => 'relation_name.related_table_field_name',
],
That's it.