Z
Z
Zohei2014-09-03 13:19:52
Yii
Zohei, 2014-09-03 13:19:52

Yii. How do you design your models?

Good afternoon.
How do you design your models for backend, frontend and other like api modules?
I will describe my difficulties using the example of the PRODUCT model.
On the one hand, there are a large number of common methods for Product classes in all modules, for example:

public function getPrice() {
    return format($this->price);
}

It seems logical to move common methods to CommonProduct and then all products to inherit from it.. SiteProduct, AdminProduct,
ApiProduct
... for the frontend in SiteActiveRecord, set defaultScopes to display only published data and be glad that the condition has been pulled up to all requests in a dozen classes.
public function defaultScope()
    {
        return [
            'condition'=>'active=1, 
        ];
    }

I want to understand what approach is the most correct
PS. To everyone who responded to the problem plus karma) marked your messages as a solution

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2014-09-03
@Zohei

Discover the DDD path. The domain model for the entire application is one and is not tied to framework modules and in general. True, in the context of Yii, it will not work to make pure models, php is still not ruby.

_
_ _, 2014-09-03
@AMar4enko

Why do you have different models for backend, frontend and api?

O
old-calm, 2014-09-08
@old-calm

In the common section, it is convenient to define common methods for working with an entity. And the specification of the entity in relation to tasks and business logic should be separated by destinations: backend, frontend, api. It is more important to separate models and business logic than models in sections, IMHO.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question