T
T
totudub2015-11-11 09:44:25
Yii
totudub, 2015-11-11 09:44:25

How to separate model logic in Yii2 advanced?

Let's say there is a Requests model in common. It has the following methods:

static function getActiveStates()
    {
        if (Yii::$app->params['isBackend']) {
            $activeStates = [
                Requests::STATE_READY,
                Requests::STATE_CONFIRM,
                Requests::STATE_CONFIRM_PAID,
                Requests::STATE_PAID
            ];
        } else {
            $activeStates = [
                Requests::STATE_NEW,
                Requests::STATE_READY,
                Requests::STATE_ATROUT,
                Requests::STATE_CONFIRM,
                Requests::STATE_CONFIRM_PAID,
                Requests::STATE_PAID
            ];
        }
        return $activeStates;
    }

    public function isActiveState()
    {
        return  in_array($this->state_id,Requests::getActiveStates());
    }

It is clear that such a division as it is now is not the best idea.
Do according to the model in the frontend and backend and inherit from the general one, or create according to the helper and pass the model to them? Maybe there are other options?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Sundukov, 2015-11-11
@alekciy

Make two independent models.

V
vitovt, 2017-03-13
@vitovt

As mentioned above - make two models. Don't be afraid to produce many models with little code if necessary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question