A
A
Alexander Fedorov2015-09-25 19:41:14
Yii
Alexander Fedorov, 2015-09-25 19:41:14

How to filter records in model before displaying index?

Good day everyone. The question is related to the question How to store and process similar entities (for example, planned and actual transactions)?
Records of two (possibly more) types of records are stored in one table.
The type is stored in the type column.
I made two menu items with the type parameter, which is passed via a get request.
I checked that the controller receives this parameter in actionIndex.
I tried to add a condition to ModelSearch - it did not help: the ['type'] parameter came in the $params variable, but after $this->load($params) it was reset to zero.
Can you please tell me how to filter the values ​​in the model (before displaying in the GridView) by the type parameter? Those. when no filters are being passed yet.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Makarov, 2015-09-25
@Pagliaccio

Any model in the load method expects an array of type:

[
    // имя класса без неймспейса
    'ModelSearch' => [
        ...
    ]
]

This behavior can be overridden, but you can also do this:
With the second parameter, we will tell the model to look for attributes from the array root.
if (!$modelSearch->type) {
    $modelSearch->type = 'булехум!';
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question