G
G
Greg Popov2016-06-07 11:15:30
Yii
Greg Popov, 2016-06-07 11:15:30

How to add data to a model in a loop in Yii2?

Hello. The essence of the problem on the fingers is as follows:
1. There are goods
2. There are technical characteristics
There can be many characteristics for one product.
I created a form from two models, the number of characteristics can be increased or decreased using js, the generated name for each new field looks like this:

name='TechSpec[0][title]'

name='TechSpec[1][title]'

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

/*
* потом loadMultiply при загрузке возвращает false если больше чем одна характеристика 
* добавлена 
* При удачной загрузке беру длину массива и циклом создавая новые объекты сохраняю      
* новые данные по индексу
*
*/

Plus, I can’t figure out how to defeat validation in new fields created using js
“Smoked” the first four pages of Google, a lot of examples on update, but not a single one on create,
I can’t understand what is my mistake?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Greg Popov, 2016-06-07
@Gregpopov

Here's the first step , here's the second step

M
Maxim Timofeev, 2016-06-07
@webinar

1. How do update examples differ from create if the save() method is both there and there, if you use the update() method, it’s another matter, but there is hardly a lot of information about it in google.
2

$techSpech = [new common\models\TechSpec]
-what is this? Why not $techSpech = new common\models\TechSpec();?
3. add the beforeSave method to the product model, iterate over your characteristics using the banal foreach and save.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question