Yii2 multi-table data insertion

Source: Internet
Author: User
Does anyone insert too many tables? {Code...} is this not acceptable? Does anyone insert too many tables?

If (Yii ::$ app-> request-> isPost) {if (Yii ::$ app-> request-> post ('test ') & $ this-> Test-> load (Yii: $ app-> request-> post () & $ this-> Test-> save ($ data )) {// var_dump ($ this-> Dish-> save (); exit; always false if (Yii: $ app-> request-> post ('dish ') & $ this-> Dish-> save () {return $ this-> render ('index', ['test' => $ this-> test, 'dish' => $ this-> dish,]);} else {var_dump ($ Dish ); // accept a value of var_dump ($ this-> Dish-> getErrors ()); // accept null value} else {var_dump ($ this-> Test-> getErrors ();} else {return $ this-> render ('index ', ['test' => $ this-> test, 'dish' => $ this-> dish,]);}

Is this not acceptable?

Reply content:

Does anyone insert too many tables?

If (Yii ::$ app-> request-> isPost) {if (Yii ::$ app-> request-> post ('test ') & $ this-> Test-> load (Yii: $ app-> request-> post () & $ this-> Test-> save ($ data )) {// var_dump ($ this-> Dish-> save (); exit; always false if (Yii: $ app-> request-> post ('dish ') & $ this-> Dish-> save () {return $ this-> render ('index', ['test' => $ this-> test, 'dish' => $ this-> dish,]);} else {var_dump ($ Dish ); // accept a value of var_dump ($ this-> Dish-> getErrors ()); // accept null value} else {var_dump ($ this-> Test-> getErrors ();} else {return $ this-> render ('index ', ['test' => $ this-> test, 'dish' => $ this-> dish,]);}

Is this not acceptable?

This is definitely not the case.

The key is: $ this-> Test-> load (Yii: $ app-> request-> post ())

Your form after a post may only correspond to one type of model. It is impossible for a form to correspond to several different models. Therefore, since you use the Test model to load all post content, you cannot use Dish to load it again.

If your form contains fields from two different models, you need to specify a primary model and add some virtual attributes to the primary model, temporarily store the value after load, and then create a new model, put these temporarily stored attribute values in the new model, and then save

Use transactions!
Post data should be differentiated by Model
For example:
Post :{

'Test' : {    'id' : 1,    'name' : 'kama'},'Dish' : {    'did' : 1,    'dname' : 'abc'}

}

Start transaction:
$ Data = Yii: $ app-> request-> post ();
If (! Empty ($ data ['test']) &! Empty ($ data ['dish'])
{

$ Transaction = \ Yii: $ app-> db-> beginTransaction (); try {$ Test = new Test (); $ Test-> load ($ data ); if ($ Test-> save () {# save Dish $ transaction-> commit () ;}else {$ transaction-> rollback ();}} catch (Exception $ e) {# rollback transaction $ transaction-> rollback ();}

}

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.