M
M
Maxim2018-06-14 18:57:32
Yii
Maxim, 2018-06-14 18:57:32

YII how to make CRUD operation inherit from base controller? How to use?

Hello! The project has a lot of repetitive actions for CRUD operations, many types with the same type of code. It would be desirable to simplify all this by means of inheritance or other method. Thus, we will get a single crud, and we will transfer dynamic data as parameters. Is there something similar? How can this be done at all? After all, then it will be necessary to create only one controller, and the views themselves will be pulled up, except for the form and _columns
Maybe it makes sense to put the CRUD actions of the controllers into classes?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2018-06-14
@myks92

I don’t understand something, probably, but this is a classic - to create a model where the necessary methods will be, and use them in the controller. Those. keep all CRUD operations in a separate class, which will be in models, connect this class in the necessary controllers and use the methods there (accessing them statically)
Example:
in frontend/models create the Crud.php file:

namespace frontend\models;

Class Crud extends Model {
    public static function getData() {
        return ... // тут какие-нибудь вычисления или берем что-то из БД и т.п.
    }
}

After that, in any controller, connect this model: And use:
$some_res = Crud::getData();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question