How to use Thinkphp--create () (Personal sentiment)

Source: Internet
Author: User

The difference between the M method and the D method

Both the M method and the D method in thinkphp are used to instantiate a model class, and the M method is used to instantiate an underlying model class efficiently, whereas the D method is used to instantiate a user-defined model class.

Using the M method

If this is the case, consider using the M method:

    1. When a simple curd operation is performed on a data table without complex business logic
    2. The M method is used in conjunction with the instantiated Commonmodel class when only the individual tables have more complex business logic

The M method can even simply look at the operation of the data table corresponding to the parameter table name:

$User = M (' User ');
Using the D method

If this is the case, consider using the D method:

    1. You need to use some of the advanced features in the thinkphp model, such as the auto-Validate function (implemented in the Create () method), the correlation model, etc.
    2. Business logic is more complex and involves many tables
    3. The business logic is defined in the Custom model class (under the Lib/model directory), and you want to implement these business logic in the operation

In addition, the D method does not support cross-project calls, and requires the use of:

$User = D (' User ', ' Admin ');    Instantiate the User model under the Admin project $user = D (' Admin.user ');        Project Grouping enabled
Tips

When item grouping is enabled, the Model class does not necessarily correspond to the grouping of items. A model class that is common among multiple project groups and is placed uniformly in the model directory and can be instantiated directly using D (' modelname '). D (' user.userinfo ') does not mean that User must be a project group, or just a catalog of files under model, and D (' user.userinfo ') instantiates the UserInfo model class under the User directory.

Summarize

Both M and D methods can be used directly in case the model class file does not exist, but the M method is obviously more efficient, but to use the business logic inside the model class, the D method must be used.

A comparative image of the analogy is: M method just like a computer installed in the operating system, can only use some basic functions, and the D method is installed on the system, such as Office, QQ and other application software, more powerful, while the whole computer running speed also slowed.

Above is a summary of the difference between M method and D method, and the M method and D method should be selected according to the actual situation.

Both M and D are the same using create ():
1 // instantiating the user model 2 $User = M (' User '); 3 // create a data object based on the post data submitted by the form 4 $User->create ();

Attention! When$User->create();来到这一步时,他会自己接受表单提交过来的数据,但是前提是表单提交的数据的name必须要和数据库里面的字段名一样才行。

Data can be modified without using the Save () or add () method, such as:

         

1 $User = M (' User '); 2 $User // creating a User data Object 3 $User // Set the default user state 4 $User  Time // set the user's creation time 5 $User // writing user objects to the database

Write Data:

1  Public functionInsert () {2     Header("content-type:text/html; Charset=utf-8 ");3     $Dao= M ("User");//instantiating a model class4 5 //Data Object Assignment6     $Dao->username = "Xiao Wang";7     $Dao->password =MD5("123456");8     $Dao->email = "[Email protected]";9     $Dao->regdate = Time();Ten  One     //Write Data A     if($lastInsId=$Dao-Add ()) { -         EchoThe Insert Data ID is:$lastInsId"; -}Else { the         $this->error (' Data write Error! ‘); -     } -}

Take it out with:

The. Create method can process the data that is submitted by the post (the data instance is automatically encapsulated by the name of the field in the table and the form submission), such as a field in the user table called "username" if there is a <input name= "in the form" Username "value=" Xiao Ming ", then $user = M (' User '); $data = $User->create (); echo $data [' username '], will output "Xiao Ming", do not use $_post[' username ' to receive.

Using the Create () method, we can use validation:

  1. Static mode: The validation rule is defined in the model class through the $_validate attribute.
  2. Dynamic: Use the Validate method of the model class to dynamically create automatic validation rules.

In any way, the definition of a validation rule is a uniform rule that defines the format as:

  1. array(
  2. array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),
  3. array(验证字段2,验证规则,错误提示,[验证条件,附加规则,验证时间]),
  4. ......
  5. );

The code above must be written in the Model folder, and the file name will be read in the manual using the ' table name (first character capitalized) Model.class.php ', the validation rules.

Automatic validation is a method of data validation provided by the Thinkphp model layer that automates data validation when creating data objects using create.

How to use

thinkphp--create () (personal sentiment)

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.