Home Backend Development PHP Tutorial The difference between shortcut keys M and D in ThinkPHP framework

The difference between shortcut keys M and D in ThinkPHP framework

Nov 24, 2016 pm 02:33 PM
thinkphp

The difference between

D() and M() methods: The main difference between

D and M is that the

M method does not need to create a model class file. The M method does not read the model class, so automatic verification is invalid by default. , but it can be achieved through dynamic assignment

And the D method must create a model class.

We can use the following two methods to create a mapping object of a data table

The first one: $Test = D('Test')

The second one: $Test = new Model('Test')

Although both can perform select, insert, delete, and udpate operations on data, there is a big difference in

data verification.

Instituting a model in the first way will have a data checking function. If the title does not have When filling in, it will prompt "Please enter the title" (this is an automatic verification function provided by tp, of course, the verification conditions need to be defined in the corresponding model);

If you use the second type, you will not have this data verification function. Manual verification is required.

The summary is as follows:

The D function instantiates the module under Lib/Model of your current project.
If the module does not exist, directly return the object of the instantiated Model (the meaning is the same as the M() function).
And M only returns the object of the instantiated Model. Its $name parameter is used as the table name of the database to handle operations on the database.

In layman’s terms:
D is to instantiate a Model based on a Model file.
M dynamically instantiates a Model object by directly instantiating the Model method (ThinkPHP base class), even if the corresponding Model file does not exist.

To put it more simply:
M instantiation parameter is the table name of the database.
D is instantiated by the model file you created under the Model folder.
D means that when you do not define a model, the system automatically defines a model for you, so that you can perform simple data input or output.
Every Action file should correspond to a Model file. If you define a Model,
For example: $Form = D("User") can be changed to $Form = new UserModel(); (User refers to your model file name).


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

How to run thinkphp project

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

There are several versions of thinkphp

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

How to run thinkphp

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

How to install thinkphp

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Which one is better, laravel or thinkphp?

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks Nov 22, 2023 pm 12:01 PM

Development suggestions: How to use the ThinkPHP framework to implement asynchronous tasks

How is the performance of thinkphp? How is the performance of thinkphp? Apr 09, 2024 pm 05:24 PM

How is the performance of thinkphp?

ThinkPHP6 backend management system development: realizing backend functions ThinkPHP6 backend management system development: realizing backend functions Aug 27, 2023 am 11:55 AM

ThinkPHP6 backend management system development: realizing backend functions

See all articles