Giter VIP home page Giter VIP logo

yii2-relation-search-filter's Introduction

Relation Search and Filtering

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).

Installation

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.

Usage

To use the trait you need to do the following (first example without using aliases):

  1. In top of your class (search-model) body put:
use \codiverum\relationSF\RelationSFTrait; 
  1. Make new public attribute in your class:
public $relation_name;
  1. Add your attribute to safe attributes
  2. In your search function add following:
  • after creating $query add:
$this->joinWithRelation($query, 'relation_name');
  • after creating $dataProvider add:
$this->addRelationSort($dataProvider, 'relation_name', 'related_table_field_name', 'table_name');
  • after creating query filters add:
$this->addRelationFilter($query, 'relation_name', 'related_table_field_name', 'table_name');
  1. Add to GridView columns array:
[
  'attribute' => 'relation_name',
  'value' => 'relation_name.related_table_field_name',
],

That's it.

Advanced Usage (with table aliases)

  1. In top of your class (search-model) body put:
use \codiverum\relationSF\RelationSFTrait; 
  1. Make new public attribute in your class:
public $relation_name;
  1. Add your attribute to safe attributes
  2. In your search function add following:
  • after creating $query add:
$this->joinWithRelation($query, 'relation_name', 'table_name', 'alias');
  • after creating $dataProvider add:
$this->addRelationSort($dataProvider, 'relation_name', 'related_table_field_name', 'alias');
  • after creating query filters add:
$this->addRelationFilter($query, 'relation_name', 'related_table_field_name', 'alias');
  1. Add to GridView columns array:
[
  'attribute' => 'relation_name',
  'value' => 'relation_name.related_table_field_name',
],

That's it.

yii2-relation-search-filter's People

Watchers

Nikolya avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.