What are the differences between D and M in ThinkPHP?
Source: Internet
Author: User
Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... Reply content: you can refer to my video... M is a model that comes with the system. you do not need to define a model. D requires a custom model. M is a quick instantiation model. you do not need to have the corresponding model class file. D is required to create a model class before instantiating according to different model Definitions. we have several methods to instantiate the model, next we will analyze the situation and the method used:
1. instantiate the basic Model class
When no model is defined, we can use the following method to instantiate a model class for operations:
$ User = new Model ('user ');
Alternatively, the M shortcut is equivalent to the M shortcut.
$ User = M ('user ');
$ User-> select (); // perform other data operations
This method is the simplest and most efficient. because no model class needs to be defined, cross-project calls are supported. The disadvantage is that there is no custom model class, so you cannot write the relevant business logic and can only perform basic CURD operations.
2. instantiate other model classes
The first method of instantiation is difficult to encapsulate some additional logic methods because there is no definition of the model class. However, in most cases, it may only need to expand some general logic, then you can try the following method.
The M method instantiates the Model class by default. to instantiate other Model classes, you can use
$ User = M ('user', 'commonmodel ');
The above method is equivalent
$ User = new CommonModel ('user ');
Because the system model classes can be automatically loaded, we do not need to manually import class libraries before instantiation. The Model class CommonModel must inherit the Model. If no alias is defined for import, it must be placed under the project Model. We can define some common logical methods in the CommonModel class to avoid defining specific model classes for each data table. if your project has more than 100 data tables, in most cases, there are some basic CURD operations, but some models have complicated business logic to be encapsulated. Therefore, the combination of the first method and the second method is a good choice.
3. instantiate a user-defined Model (××model) class
This situation is the most widely used. if a project inevitably needs to define its own business logic implementation, it needs to define a model class for each data table, such as UserModel and InfoModel.
The defined Model classes 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, the Model class can inherit a user-defined public Model class, rather than only inherit the Model class.
To instantiate a custom model class, you can use the following method:
$ User = new UserModel ();
Or use D shortcut to instantiate is equivalent
$ User = D ('user ');
$ User-> select (); // perform other data operations
The D method can automatically detect Model classes. if a custom Model class exists, the user-defined Model class is instantiated. if the class does not exist, the Model base class is instantiated, at the same time, models that have already been instantiated will not be instantiated repeatedly. The default D method can only call models of the current project. to call models of different projects, you need:
$ User = D ('user', 'admin'); // instantiate the User model under the Admin project
$ User-> select ();
If the module grouping function is enabled, you can use:
$ User = D ('admin. user ');
4. instantiate an empty model class
If you only use native SQL queries, you can instantiate an empty model class without using additional model classes. for example:
$ Model = new Model ();
// Or the M shortcut is equivalent for instantiation.
// $ Model = M ();
$ Model-> query ('select * FROM think_user where status = 1 ');
The empty model class can also be called across projects.
The D function must have a Model file and M does not need it. Instead, it only generates a basic Model object that can operate on database tables. The simple and rough way to understand is: M is to directly instantiate a table, and D is to mount a model.
Php advanced QQ group Welcome to the 474370592m method is relatively simple and inflexible to model encapsulation. D is generated by calling the corresponding module, which is more flexible. For example, you can use the d method to modify the name of the called table and call methods in different modules.
In addition, you can rewrite the constructor of the original method in the model called by method d, and set other methods, such as the ing method. It also needs to be set. For example, you can perform detailed settings in the class file of the corresponding model module after calling the data table in method d.
Protected $ _ scope = array (); can be used to set select search conditions
Protected $ _ validate = array (); can be used to create validation rules for data before it is submitted to the database.
In this way, the D method is more flexible than the M method, and the code is easier to read. the relevant data operation is executed in Part C, and the relevant data verification, the retrieval conditions are in the M aspect. D does not have to have a corresponding model class. you can instantiate a method that contains classes for objects initialized by the null D method, the M method only initializes the object and only contains the database model operation methods defined in Thinkphp. To put it simply, D applies to the initialization of abstract classes, while M is the original ORM.
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