In-depth exploration of the Create method in Thinkphp

Source: Internet
Author: User

I encountered a problem in the create () method of thinkPHP for working reasons, so I tracked the create () method and further explored the create () method.
OriginalThe create () method originally has two parameters.,The first parameter is a well-known data parameter.,The second is the hidden $ type parameter.So what is this parameter used to control?
// Status $ type = $ type? $ Type! Empty ($ data [$ this-> getPk ()])? Self: MODEL_UPDATE: self: MODEL_INSERT );
After carefully pondering this sentence, we found that,This hidden parameter indicates the specific operation of the database. 1 indicates the insert operation, and 0 indicates the update operation.,
By default, you do not need to assign a value to this parameter because the system can automatically identify it.

It is identified as follows:

If the input data contains the same fields as the primary key, the database operation is an update operation by default. This is mainly because the primary key is auto-incrementing by default in most cases, insert operations generally do not assign values to the primary key, but the problem lies here.


Recently, the student ID is used as the primary key, but the student ID cannot use auto-increment, but has a fixed format and must be entered,
However, the system automatically regards my input operation as an update operation, and my Automatic completion code is written like this:
Copy codeThe Code is as follows: protected $ _ auto = array (
Array ('majorid', 'maxmajoridadd1 ', 1, 'callback '),
);

The third parameter 1 can be found in the Manual that the automatic operation is executed during the insert operation.
The system regards my insert operation as an update operation, and the automatically completed code I set will naturally become invalid and will not be executed.

When you want to enter the value of the primary key field, you can write

Copy codeThe Code is as follows: create ($ _ POST, 1)

Directly tell the create method that this operation is an insert operation.

This is a very hard-to-find problem. Many people have encountered this problem recently.

In addition, the function of automatic verification/Automatic completion is invalid. It may be because your Model class name is wrong or the like. I have made this mistake, and it is common to have multiple letters and fewer letters.

Basically, automatic verification/Automatic completion failure are both possible.

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.