ThinkPHP supports hierarchical model

ThinkPHP supports hierarchical model, in addition to the Model layer, we need to design and create additional layers of model projects.

Marble platform bracket

Typically, different hierarchical model is still \ of Think \ Model class or subclass inherits the system, therefore, its operation and the basic operation of the Model class is the same.

In the design example Home module need to distinguish between different layers of the model data layer, a logical layer, a service layer, etc., we can create the following modules directory Model, Logicand Servicedirectories, the model for operation of all user tables is divided into three layers:

  • Data layer: Home \ Model \ UserModel data for defining the relevant automatic validation and data access interface and automatically
  • Logical layers: Home \ Logic \ UserLogic used to define user-related business logic
  • Service Layer: Home \ Service \ UserService for services related to user interface definition, etc.

Three layer model is defined as follows:

Model类:Home\Model\UserModel.class.php

  1. namespace Home\Model;
  2. class UserModel extends \Think\Model{
  3. }

Examples of methods:D('User');

Logic类:Home\Logic\UserLogic.class.php

  1. namespace Home\Logic;
  2. class UserLogic extends \Think\Model{
  3. }

Examples of methods:D('User','Logic');

Api类:Home\Api\UserApi.class.php

  1. namespace Home\Api;
  2. class UserApi extends \Think\Model{
  3. }

Examples of methods:D('User','Api');

Method D model layer by the default operation of DEFAULT_M_LAYERthe configuration parameters, we can change the default operation model layer Logic layer, for example:

  1. 'DEFAULT_M_LAYER' => 'Logic', // 默认的模型层名称

In this way, when we call:

  1. $User = D('User');

When in fact is an instance of the  UserLogicclass, instead of UserModelclass.

 

Guess you like

Origin www.cnblogs.com/furuihua/p/11813569.html