What is the difference between D and M in thinkphp?

Source: Internet
Author: User

Reply content:

You can refer to my video ... M is a model that comes with the system, does not require a custom model, and D, if you need to customize it. M is a fast instantiation model, does not require you to have the corresponding model class file, D is necessary to create a model class to instantiate according to different model definitions, we have several methods of instantiating the model, below to analyze under what circumstances use what method:

1. Instantiation of the Basic model class
When no model is defined, we can instantiate a model class using the following method:
$User = new Model (' User ');
Or using the M shortcut method instantiation is equivalent
$User = M (' User ');
$User->select (); Perform other data operations
This approach is the simplest and most efficient because there is no need to define any model classes, so cross-project calls are supported. The disadvantage is also that because there is no custom model class, the related business logic cannot be written and only basic curd operations can be done.

2. Instantiation of other model classes
The first method is instantiated because there is no definition of the model class, so it is difficult to encapsulate some additional logical methods, but in most cases, perhaps just need to extend some common logic, then you can try one of the following methods.
The M method defaults to instantiating the model class, and if you need to instantiate another model class, you can use the
$User = M (' User ', ' Commonmodel ');
The above method is equivalent to
$User = new Commonmodel (' User ');
Because the model classes of the system can be loaded automatically, we do not need to manually perform class library import operations prior to instantiation. The model class Commonmodel must be inherited, and should be placed under the project model if no aliases are defined for import. We can define some common logic methods in the Commonmodel class, and we can omit to define a specific model class for each data table, if your project already has more than 100 data tables, and most of the cases are some basic curd operations, Just the individual models have some complex business logic that needs to be encapsulated, so the combination of the first and second approaches is a good choice.

3. Instantiate a user-defined model (Xxxmodel) class
This situation is the most used, a project inevitably needs to define its own business logic implementation, you need to define a model class for each data table, such as Usermodel, Infomodel, and so on.
The model classes that are defined are usually placed under the Lib\model directory of the project. For example
Class Usermodel extends model{
Public Function Myfun () {
Add your own business logic
.........
}
}
In fact, model classes can also inherit a user-defined public model class, rather than inheriting only the model class.
To instantiate a custom model class, you can use the following method:
$User = new Usermodel ();
or using the D shortcut method instantiation is equivalent
$User = D (' User ');
$User->select (); Perform other data operations
The D method automatically detects the model class and, if there is a custom model class, instantiates the custom model class and, if not, instantiates the model base class, and does not repeat the instantiation for the instantiated models. The default D method can only support the model that invokes the current project, and if a cross-project call is required, use:
$User = D (' User ', ' Admin '); Instantiate the user model under the Admin project
$User->select ();
If the module grouping feature is enabled, you can use:
$User = D (' Admin.user ');

4. Instantiation of empty model classes
If you are only using native SQL queries, you do not need to use an extra model class to instantiate an empty model class, for example:
$Model = new Model ();
Or using the M shortcut method instantiation is equivalent
$Model = M ();
$Model->query (' SELECT * from Think_user where Status=1 ');
The empty model class also supports cross-project invocation.
The D function must have the model file present, m not required, just generate a most basic model object that can operate on the database table. The simple and rough way to understand this is that M is directly instantiating a table and D is mounting a model
PHP Advanced QQ Group Welcome to join the 474370592m method is relatively simple and inflexible for model encapsulation. D is to call the corresponding module based on the build, more flexible. such as modifying the name of the calling table, invoking methods under different modules can use the D method.
In addition, you can override the constructor of the original method in the model called by the D method, and set up other methods, such as the mapping method. It also needs to be set up. For example, you can set the details in the class file under the model module by invoking the data table in the D method.
protected $_scope=array (); a condition that can be used to set a select search
protected $_validate=array (); can be used to create validation rules for data before committing to the database.

So the D method is more flexible than the M method, the code is notconsistent easy to read, the data operation part is executed in part C, and the relevant data validation, retrieval conditions, etc. are carried out in M. D not necessarily have the corresponding model class, you can instantiate an empty D-D method initializes the object containing the method of the class, and the M method only initializes the object containing only the thinkphp internal definition of the database model operation method. It is easy to say that D applies to the initialization of an abstract class, while M is the original ORM.
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.