ThinkPHP supports hierarchical model, in addition to the Model layer, we need to design and create additional layers of model projects.
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
, Logic
and Service
directories, 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
namespace Home\Model;
class UserModel extends \Think\Model{
}
Examples of methods:D('User');
Logic类:Home\Logic\UserLogic.class.php
namespace Home\Logic;
class UserLogic extends \Think\Model{
}
Examples of methods:D('User','Logic');
Api类:Home\Api\UserApi.class.php
namespace Home\Api;
class UserApi extends \Think\Model{
}
Examples of methods:D('User','Api');
Method D model layer by the default operation of DEFAULT_M_LAYER
the configuration parameters, we can change the default operation model layer Logic layer, for example:
'DEFAULT_M_LAYER' => 'Logic', // 默认的模型层名称
In this way, when we call:
$User = D('User');
When in fact is an instance of the UserLogic
class, instead of UserModel
class.