没有不值得去解决的问题,也没有不值得去学习的技术!

Implementing Model Layering in the Yii 2.0 Advanced Template: Data and Logic Layers

在common/logics/redis目录中的Redis(ActiveRecord)模型文件为业务逻辑相关,继承至 \common\models\redis\Game 数据层

1. Definitions:
Data layer: models defines data-related automatic validation, automatic completion, and data access interfaces.
Logic layer: logics defines data-related business logic.

2. Create a logics directory in the common directory to serve as the location for the logic layer of MySQL models, as shown in Figure 1.

Create a logics directory in the common directory to serve as the location for the logic layer of MySQL models
Figure 1

3. Create a redis directory inside the common/logics directory to serve as the location for the logic layer of Redis (ActiveRecord) models, as shown in Figure 2.

Create a redis directory inside the common/logics directory to serve as the location for the logic layer of Redis (ActiveRecord) models
Figure 2

4. The MySQL model files in the common/models directory are generated by the Gii tool, as shown in Figure 3.

The MySQL model files in the common/models directory are generated by the Gii tool
Figure 3

5. The Redis (ActiveRecord) model files in the common/models/redis directory define the model field structure, as shown in Figure 4.

The Redis (ActiveRecord) model files in the common/models/redis directory define the model field structure
Figure 4

6. The MySQL model files in the common/logics directory handle business logic and inherit from the \common\models\Game data layer, as shown in Figure 5.

api\models
common\logics

The MySQL model files in the common/logics directory handle business logic and inherit from the \common\models\Game data layer
Figure 5

7. The Redis (ActiveRecord) model files in the common/logics/redis directory handle business logic and inherit from the \common\models\redis\Game data layer, as shown in Figure 6.

api\models\redis
common\logics\redis

api\models
common\models

Api
Common

common\models\redis\Lock
common\logics\redis\Lock

common\models\redis\Live
common\logics\redis\Live

$api
$common

The Redis (ActiveRecord) model files in the common/logics/redis directory handle business logic and inherit from the \common\models\redis\Game data layer
Figure 6

8. The Redis (native command) model files in the common/logics/redis directory handle business logic. They do not need to inherit from the \common\models\redis\Game data layer; instead, they inherit directly from \yii\redis\ActiveRecord, as shown in Figure 7.

The Redis (native command) model files in the common/logics/redis directory handle business logic. They do not need to inherit from the \common\models\redis\Game data layer; instead, they inherit directly from \yii\redis\ActiveRecord
Figure 7

9. The MySQL model files in the api/models directory handle business logic and inherit from the \common\logics\Game logic layer, as shown in Figure 8.

\common\models
\common\logics

The MySQL model files in the api/models directory handle business logic and inherit from the \common\logics\Game logic layer
Figure 8

10. The Redis (ActiveRecord/native command) model files in the api/models/redis directory handle business logic and inherit from the \common\logics\redis\Game logic layer, as shown in Figure 9.

common\models\redis\Lock
common\logics\redis\Lock

\common\models\redis
\common\logics\redis

The Redis (ActiveRecord/native command) model files in the api/models/redis directory handle business logic and inherit from the \common\logics\redis\Game logic layer
Figure 9

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系