In Yii2 how to add data to the model cycle?

Hi. The essence of the problem on the fingers is as follows:

1. There are products
2. There are specifications
To a single product may have many characteristics.

Created the form of two models, the number of characteristics can increase or decrease with js generated name each new field looks like this:

name='TechSpec[0][title]'

name='TechSpec[1][title]'


The object-based model is created like this:
$techSpech = [new common\models\TechSpec]

/*
* then loadMultiply when loading returns false if more than one feature 
* added 
* After a successful download of take the length of the array and the cycle of creating new objects retain 
* the new index data
*
*/


Plus can't figure out how to defeat the validation new created using js fields

"Smoked" the first four pages of Google, plenty of examples in the update, but no create,
can't understand what my error is?

Thanks in advance.
July 8th 19 at 16:02
2 answers
July 8th 19 at 16:04
Solution
July 8th 19 at 16:06
1. Than update the examples differ from create if here and there the save () method, but if You use the method update(), is another matter, but it is unlikely on it is a lot of information to google.
2
$techSpech = [new common\models\TechSpec]
-what is it? Why not $techSpech = new common\models\TechSpec();?
3. added to the product model in beforeSave method, it plucked its own characteristics with the help of banal foreach and save.
1. Meant classic updateAction where all the indices indicated and only to take and keep
2. For loadMultiply that accepts an indexed array
3. Not very convenient to score a model with such one-time needs. - norene.Nikolaus82 commented on July 8th 19 at 16:09
so here create
//Send at least one model to the form
$products = [new Product()];

//Create an array of the products submitted
for($i = 1; $i < $count; $i++) {
$products[] = new Product();
} - norene.Nikolaus82 commented on July 8th 19 at 16:12
if a fuller example, here so

//Find out how many products have been submitted by the form
$count = count(Yii::$app->request->post('Product', []));

//Send at least one model to the form
$products = [new Product()];

//Create an array of the products submitted
for($i = 1; $i < $count; $i++) {
$products[] = new Product();
}

//Load and validate the multiple models
if (Model::loadMultiple($products, Yii::$app->request->post()) && Model::validateMultiple($products)) {

foreach ($products as $product) {

//Try to save the models. Validation is not needed as it's already been done.
$product->save(false);

}
return $this->redirect('view');
} - norene.Nikolaus82 commented on July 8th 19 at 16:15
: 1. Classic updateAction is фсешщтUpdate? What intecsa there checked?
2. loadMultiply - in your case not needed
3. That means one-time need? Then just put your database without frameworka handles if it is a 1 time need. - Henderson_Beat commented on July 8th 19 at 16:18
: "Validation is not needed as it's already been done." - where does the validation - I do not see. - Henderson_Beat commented on July 8th 19 at 16:21
: Why count and if there is foreach for? - Henderson_Beat commented on July 8th 19 at 16:24
: why $products = [new Product()]; if you need $products = new Product(); - Henderson_Beat commented on July 8th 19 at 16:27
: did this example: stackoverflow.com/questions/32481399/yii2-insert-m... - norene.Nikolaus82 commented on July 8th 19 at 16:30
: when updating the data, you have removed all the har-Ki an array, no need to generate js - norene.Nikolaus82 commented on July 8th 19 at 16:33
: I have the impression that either I don't understand what You are doing, or You the 2nd time in my life I see yii. - Henderson_Beat commented on July 8th 19 at 16:36
In your link example of how to add multiple items to one form, in Your case You have not products, and features, as follows from the description belonging to the 1 merchandise. - Henderson_Beat commented on July 8th 19 at 16:39
Therefore model 1 for the item, it creates public $tech_har = [], it zapihivaetsya in the form of elements that move in the beforeSave of the model of the product - Henderson_Beat commented on July 8th 19 at 16:42
: I create a form from 2 models: the first item, the second characteristics - norene.Nikolaus82 commented on July 8th 19 at 16:45
: and I need to shove at a time (cycle) being attached via a key of the product to a bunch of characteristics - norene.Nikolaus82 commented on July 8th 19 at 16:48
: beforeSave in the model of the product - Henderson_Beat commented on July 8th 19 at 16:51
: you have not delved into the issue - norene.Nikolaus82 commented on July 8th 19 at 16:54

Find more questions by tags ORMYii