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.