Differences between the instance Model Method in thinkphp

Source: Internet
Author: User

In TP, we can use the following two methods to create a ing object for a data table (I am currently using)
First: $ test = D ('test ')
Type 2: $ test = new model ('test ')
Although both of them can perform select, insert, delete, and udpate operations on data, there is a big difference in data verification,
Let's take a look at the effect. First, create a testmodel. CopyCodeThe Code is as follows: Class testmodel extends model {
Protected $ _ validate = Array {
Array ('title', 'require ', 'Enter the title', 1 ),
Array ('content', 'require ', 'Enter the content', 1 ),
}
}

Create a testactionCopy codeThe Code is as follows: Class testaction extends action {
Public Function Dtest (){
$ Test = D ('test'); // first case
$ Test = new model ('test'); // The second case
If ($ test-> Create ()){
$ Test-> Add ();
} Else {
$ Test-> geterror ();
}
}
}

When running, you will find that the data check function is available for a model instance in the first method, if the title is not entered, the system prompts "Enter the title" (this is an automatic verification feature provided by TP, and the verification criteria must be defined in the corresponding model ); if the second method is used, there will be no ·····

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.