Why save() srabatyvaet once?

there is an array $catID
here are his values

array(2) { 
 [0]=> string(1) "1" 
 [1]=> string(1) "2"
 }


try to start it to save using foreach here is the code



foreach($catID as $cat){
$catSerial->id_cat=$cat;
$catSerial->id_serial=$model->id;
$catSerial->save();
 }


the problem is that save() worked through one last time why ? And how to make it work twice.
July 12th 19 at 17:31
3 answers
July 12th 19 at 17:33
Solution
What I would like to say in the first place. Calling save() entails the query in the database. You call this method in a loop, and it well, actually not correct. I would recommend to make the desired functionality a different way.
Sql queries in a loop to do the impossible !!!
plus the fact that the same pattern continues in a loop and overwriting the old data - giovani_Oberbrunner98 commented on July 12th 19 at 17:36
in fact, you can do queries in a loop, technically. But it's called bad code. It would be correct to say: - "nastoyatelno not recommended in order to improve performance" - Georgette_Goyette commented on July 12th 19 at 17:39
How best ? - rozella92 commented on July 12th 19 at 17:42
: stackoverflow.com/questions/452859/inserting-multi... - Kendrick_Schu commented on July 12th 19 at 17:45
: how do I do it INSERT INTO MyTable ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 ) make ActiveRecord method? - rozella92 commented on July 12th 19 at 17:48
: Walk cycle on your array and make the INSERT query.
Don't know what you have a framework, but there needs to be any method that performs a query to the database. Something like this query($sql). Call this method and pass in there you created the request. - Kendrick_Schu commented on July 12th 19 at 17:51
I fraimwork yii2 - rozella92 commented on July 12th 19 at 17:54
: I understand that. as the question in the "Yii"))
I've been in Yii is not working. Look something like this:
$db->createCommand('Insert ... ')
->execute(); - Kendrick_Schu commented on July 12th 19 at 17:57
: stackoverflow.com/questions/27355262/activerecord-... - donna.Fisher commented on July 12th 19 at 18:00
: I do not agree with your statement about the request in a loop. It is clear that the author of something strange going on, but in General... bathInsert only optimizes the query, but this in itself is not always the most important. As a rule, much more important is the validation, handling events, performing any business rules and other business logic when inserting/changing. So the rule "sql Queries in a loop to do" in fact not always applicable, it depends on the context. - Harry.Christians commented on July 12th 19 at 18:03
: I have to disappoint You, but You are wrong. Validation, business rules, etc. etc. it's all very important, do not argue. But the queries in a loop to do it. It is always applicable and cannot be discussed. - Kendrick_Schu commented on July 12th 19 at 18:06
batchInsert worked thank you all - rozella92 commented on July 12th 19 at 18:09
: You can, in a single transaction, drop in performance compared to batch are minimal, if at all. - maxie.Mo commented on July 12th 19 at 18:12
: What does the transaction. If it the array is 10,000 elements, then the script will run half an hour, if at all, will work. - Kendrick_Schu commented on July 12th 19 at 18:15
July 12th 19 at 17:35
Sense, it only works one time?
Occurs only record the last item? or written to all but only at the end?
what you have primarykey in catSerial ?
is the entry of the last element - giovani_Oberbrunner98 commented on July 12th 19 at 17:38
without seeing code it's hard to say what You want, but try to look at it
stackoverflow.com/questions/29272150/insert-multip...

your problem is that You continuously update/maintain one model, but You probably need to add a new record. that is, for each iteration, or create a model, or tighten the existing one to update. - Georgette_Goyette commented on July 12th 19 at 17:41
: Yes you are right I just need. But I have not found the solution. - rozella92 commented on July 12th 19 at 17:44
: such a plan is the query INSERT INTO MyTable ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2), how to make - Kendrick_Schu commented on July 12th 19 at 17:47
: stackoverflow.com/questions/7048144/how-do-you-ins...
$values = '(null, "your string"), (null, "next string"), (null, "third string")';
$sql = 'INSERT INTO table_data (id, data) VALUES' . $values;
$command = Yii::app()->db->createCommand($sql);
$command->execute();

or so
stackoverflow.com/questions/27355262/activerecord-... - rozella92 commented on July 12th 19 at 17:50
July 12th 19 at 17:37
You probably have an error in the logic of the given code. The feeling that you want to create 2 rows in the database, but the given code will do one, and then change it. You'd better write what you want to do.

Just keep in mind: save invoked in any case, but not always it will save. For example, if not passed validation, it will not attempt to save an invalid model to the database.

It is quite possible that you have a unique validator on the cat_id and the row with that id already exists - do not save to save and just returns false.

If you do something critical, the result save always need to check! And either throw acceps, or somehow it to process.

Find more questions by tags Yii