Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

Yii2 GridView Filtering on Relational Column

class UserSearch extends User
{
    public $company_name;
    public function rules()
    {
        return [
            [['first_name', 'last_name', 'email', 'company_name'], 'safe']
        ];
    }

    public function search()
    {
        $query = User::find();
        $query->joinWith(['client c']); //set relation alias

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query
            ->andFilterWhere(['like', 'first_name', $this->first_name])
            ->andFilterWhere(['like', 'last_name', $this->last_name])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', "c.company_name", $this->company_name]);

        return $dataProvider;
    }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #GridView #Filtering #Relational #Column
ADD COMMENT
Topic
Name
3+5 =