THINKPHP5 model Batch increase data small record

Source: Internet
Author: User

Landlord recently in learning thinkphp5, really did not should be the general use of the tutorial said: You better just never learned thinkphp3.2. Otherwise the distress is heavy.

Because I want to implement some functions once, so I wrote a file upload class.

Can implement single file, multi-file upload (file or picture).

In view of the thinkphp 5 notation, see the document is the new $model->save () method. If this is a batch increase, there are two ways to do it:

Procedure one: $model->saveall ($data);

Procedure two: $model->isupdate (False)->save ();//Cycle

This is what the authorities are giving.

Prototype:

1 //Single data2 //method 13 $user=NewUser;4 $user->name = ' thinkphp ';5 $user->email = ' [email protected] ';6 $user-save ();7 8 //Method 29 //using the Model helper function to instantiate the user modelTen $user= Model (' User '); One //assigning values to model objects A $user-data ([ -' Name ' = ' thinkphp ', -' Email ' = ' [email protected] ' the ]); - $user-save (); -  - //Multiple Data Additions + //method 1 - $user=NewUser; + $list= [ A[' Name ' = ' thinkphp ', ' email ' = ' [email protected] ', at[' Name ' = ' onethink ', ' email ' = ' [email protected] '] - ]; - $user->saveall ($list); -  - //Method 2 - $user=NewUser; in $list= [ -[' ID ' =>1, ' name ' = ' thinkphp ', ' email ' = ' [email protected] ', to[' ID ' =>2, ' name ' = ' onethink ', ' email ' = ' [email protected] ', + ]; - $user->saveall ($list,false);

I do not know what you think, anyway, I followed the previous practice, the direct use of $model->save ($data); Add a new data, did not expect to really succeed.

Then the idea of inertia, the cycle of time using the $model->isupdate (fasle)->save ($data);

And then? No, then, sad:

1  for Key ' PRIMARY '

The display is obvious. There are data that prevent the data from being added. If you go from the top of Baidu to find a solution from this error area, it is estimated to teach you to set the primary key as the self-increment field.

However, the obvious landlord in the design of the data table, the primary key has been a sub-increment field, let alone before you can add data!

It's really magical. Then I went to the database to see the record of the data, but also really found that there is a record inside, but this record in addition to the primary key, the rest of the only default information.

Guess, in the execution of the process, the execution of two new, do not know why the second time, the value of the primary key does not know why the first time with the new value of the association.

The landlord does not practice enough to delve into. Then look at the comments below the document to find a workaround.

Code when the error is executed:

1 //New2      Public functionTest_add () {3         $data= [4' Name ' = ' TM ',5             //' score ' =6' iphone ' = ' 13631789377 ',7' Email ' = ' [email protected] ',8' Status ' = 19         ];Ten  One         $student= Model (' Student '); A         //a record -         $res=$student->save ($data); -Dump$student-ID); the         //more than one record -          for($i= 0;$i< 4;$i++){ -             $res=$student->isupdate (false)->save ($data); -Dump$student-ID); +         } -}

This is just to test a new data table.

Is the code that successfully inserts the data:

1  //New2      Public functionTest_add () {3         $data= [4' Name ' = ' TM ',5             //' score ' =6' iphone ' = ' 13631789377 ',7' Email ' = ' [email protected] ',8' Status ' = 19         ];Ten  One         $student= Model (' Student '); A         //Single record -         $res=$student->save ($data); -Dump$student-ID); the  -         //more than one record -          for($i= 0;$i< 4;$i++){ -             $res=$student->data ($data,true)->isupdate (false),save (); +Dump$student-ID); -         } +}

See from the code, the data is placed in front of the use of the message passed, as for the second parameter, I see the discussion area someone passed in the wrong, but the author said please use the latest code ~ ~ ~ for the elder brother observed a few minutes of silence.

But I do not set the second parameter are tried, did not find anything different. If the code changes later. Please take my current version of the subject ~ ~ ~ Sweat!!

Summarize the experience of learning TP5:

Watched for one weeks, intermittent, and repeatedly. It can only be said that the author still needs to complete the document. After all, the poor students such as the landlord is still a lot of-no money to see the documents of the fee.

The final point is--please update your code in a timely manner. Don't look at the document and say what it is. Be sure to do it, because you don't know if the code has been updated in minutes. (Now I'm just going to git their project back)

The more important point is: when trying to solve the problem, go to the comments below the document, perhaps the answer is where. It's true. This is how I find the data function for multiple records!

THINKPHP5 model Batch increase data small record

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.