I
I
im_dimas2018-10-12 12:39:37
Yii
im_dimas, 2018-10-12 12:39:37

How to setup filter in yii2?

Hi friends. I came across a project written in yii2 by a programmer who did not leave a single comment in the code. Plus, I have never worked with yii2 projects before.
I'll get down to business. The task is this: you need to make (or rather complete) a filter by name, which are respectively registered in the Mysql database table. I found a filter script:

...
            ->andFilterWhere(['like', 'documents.contract_number', $this->contractNumber])
            ->andFilterWhere(['like', 'companies.request_number', $this->requestNumber])
            ->andFilterWhere(['program.type' => $this->service])
            ->andFilterWhere(['manager_id' => $this->manager_id])
            ->andFilterWhere(['work_status' => $this->work_status])
            ->andFilterWhere(['companies.came_from' => $this->cameFrom])
            ->andFilterWhere(['inn' => $this->inn])
...

and accordingly, by analogy, based on the structure of the database, added the following condition:
->andFilterWhere(['like','program.name' , $this->mainProgram])

everything would be fine if it were not for error number 42.
The structure of the database is exactly the same, I suspect that somewhere in the code there is roughly an array with aliases for the names of tables and database fields. It may seem funny to experienced yii's, but I need help, I've been fiddling with this for a very long time.
PS: if I use program.name - program.id together in the filter, it works, but accordingly it searches by ID, and not by name, tell me where to dig?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-10-12
@dimas199862

Good afternoon.
You need to add a little search model.
Add a public property to this model. This property will then be displayed in the gridView and filtered by it.
Approximately like this:

public $program_name_id; // необходимо добавить в правила rules(), например указать, что это свойство integer

public function rules()
{
   return [
        ['program_name_id', 'integer']
   ];
}

// и добавить в фильтры
->andFilterWhere(['program.id' => $this->program_name_id])

Now it's not entirely clear here, most likely "program" is a relationship between models and tables in the database.
If so, then in the gridview you can display it like this:
'columns' => [
   [
     'attribute' => 'program_name_id',
     'value' => 'program.name'
   ]
]

And add a filter
'columns' => [
   [
     'attribute' => 'program_name_id',
     'filter' => Program::getAllProgram()
     'value' => 'program.name'
   ]
]

Program::getAllProgram() in the Program model might look like this
use yii\helper\ArrayHelper;
public static function getAllProgram()
{ 
    /**
   *  в запрос self::find()->all() можно добавить условие выборки, сортировки, группировки и т.д. и т.п.
   */
     return ArrayHelper::map(self::find->all(), 'id', 'name');
}

ps What would be searched for by name, I think what needs to be changed in the search model and in the view like this:
public $program_name; // необходимо добавить в правила rules(), например указать, что это свойство string

public function rules()
{
   return [
        ['program_name', 'string']
   ];
}

// и добавить в фильтры
->andFilterWhere(['like','program.name' , $this->program_name])

If so, then in the gridview you can display it like this:
'columns' => [
   [
     'attribute' => 'program_name',
     'value' => 'program.name'
   ]
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question