search
HomeBackend DevelopmentPHP Tutorial ThinkPHP快捷步骤D和M的区别

ThinkPHP快捷方法D和M的区别

简单说使用M 方法的话,是不需要定义对应的模型类的(即使有定义也不会读取),通常这样的模型仅能调用一些系统基础模型类Model类里面的一些方法。而使用D方法实例化模型的话,必须有对应的模型类文件,可以调用一些模型自定义的方法或者属性,另外在Mode对应文件里面有自动验证或者函数之类的业务逻辑也必需用D

再打个比方说M是刚安装好的操作系统,只有系统自带的应用 还没有自己安装的应用(所以只能调用内置的Model提供的属性和方法)D是已经安装了很多的第三方的应用程序 使用起来更丰富一些(可以调用模型类自己定义的属性和方法)但是很明显,安装了很多的第三方应用后系统性能降低了,执行变慢了,但是功能显然强大了。

?

D方法用于快速创建模型对象的实例,并且单例化,例如:

$User = D("User");

等效为

$User = new UserModel();

?

m实例化参数是数据库的表名.
d
实例化的是你自己在model文件夹下面建立的模型文件.

?

DM的区别主要在于:

M方法不需要创建模型类文件,M方法不会读取模型类,所以默认情况下自动验证是无效的,但是可以通过动态赋值的方式实现;

D方法必须有创建模型类,我们可以用下面两种方法去创建一个数据表的映射对象

第一种:$Test = D('Test')

第二种:$Test = new Model('Test')

虽然这两种都可以对数据进行select,insert,delete,udpate操作,在数据验证上有很大的不同,用第一种方式实例一个模型就会有数据检查功能,如果 title 没有填写的话就会提示请输入标题(这个是tp提供的一个自动验证功能,当然也需要在相应的model中定义好验证条件);

D方法可以自动检测模型类,不存在时他会抛出一个异常。同时对于已实例化得得模型不会再去重复实例化(单例)。

默认的D方法只能支持调用当前的项目(或者叫做应用)下的模型

?

例如:$user = new UserModel();

等价于$user = D('user');

?

如果实例化的是一个空模型

例如 $Demo = new Model();

那么它等价于 $Demo = M();

Statement
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
What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

What is the importance of setting the httponly flag for session cookies?What is the importance of setting the httponly flag for session cookies?May 03, 2025 am 12:10 AM

Setting the httponly flag is crucial for session cookies because it can effectively prevent XSS attacks and protect user session information. Specifically, 1) the httponly flag prevents JavaScript from accessing cookies, 2) the flag can be set through setcookies and make_response in PHP and Flask, 3) Although it cannot be prevented from all attacks, it should be part of the overall security policy.

What problem do PHP sessions solve in web development?What problem do PHP sessions solve in web development?May 03, 2025 am 12:02 AM

PHPsessionssolvetheproblemofmaintainingstateacrossmultipleHTTPrequestsbystoringdataontheserverandassociatingitwithauniquesessionID.1)Theystoredataserver-side,typicallyinfilesordatabases,anduseasessionIDstoredinacookietoretrievedata.2)Sessionsenhances

What data can be stored in a PHP session?What data can be stored in a PHP session?May 02, 2025 am 12:17 AM

PHPsessionscanstorestrings,numbers,arrays,andobjects.1.Strings:textdatalikeusernames.2.Numbers:integersorfloatsforcounters.3.Arrays:listslikeshoppingcarts.4.Objects:complexstructuresthatareserialized.

How do you start a PHP session?How do you start a PHP session?May 02, 2025 am 12:16 AM

TostartaPHPsession,usesession_start()atthescript'sbeginning.1)Placeitbeforeanyoutputtosetthesessioncookie.2)Usesessionsforuserdatalikeloginstatusorshoppingcarts.3)RegeneratesessionIDstopreventfixationattacks.4)Considerusingadatabaseforsessionstoragei

What is session regeneration, and how does it improve security?What is session regeneration, and how does it improve security?May 02, 2025 am 12:15 AM

Session regeneration refers to generating a new session ID and invalidating the old ID when the user performs sensitive operations in case of session fixed attacks. The implementation steps include: 1. Detect sensitive operations, 2. Generate new session ID, 3. Destroy old session ID, 4. Update user-side session information.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft