G
G
galithr2016-08-08 11:57:40
Yii
galithr, 2016-08-08 11:57:40

How to properly organize the filter form?

There are ActiveRecord users (User):

class User extends  \yii\db\ActiveRecord {

    public function rules(){
        return[
            [
                 'first_name',
                 'last_name',
                  'login'
            ],
            'string'
      ];
   }
}

The user list page has a filter form that is organized by \yii\base\Model.
class UserFilter extends \yii\base\Model {

    public $first_name;
  
    public $last_name;
}

Accordingly, the user enters data using it and sends it to the server, and filtering is already taking place there. Actually, the question is where is it more correct to place the filtering method (forming a query with conditions) in ActiveRecord, for example:
public static function SearchByFilter(UserFilter $filter) {
  return self::find()->where(['last_name' => $filter->last_name, 'first_name' => $filter->first_name])
}

or in the UserFilter model, for example:
public function Search(){
  return User::find()->where(['last_name' => $this->last_name, 'first_name' => $this->first_name])
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita, 2016-08-08
@galithr

What is wrong with the standard implementation? Which is generated through gii or is in the templates.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question