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 ();}
}