How to save data in database?

I use yii2 widget-multiple-input, some do not understand in the documentation how to save data in the database neskolbko values
in the guide if you want all to view the model and the picture
Implemented that way, but in the end
$models = [new Custom()];
$request = Yii::$app->getRequest();

Model::loadMultiple($models, Yii::$app->request->post())) {
 $data = Yii::$app->request->post('Custom', []);
 foreach (array_keys($data) as $index) {
 $models[$index] = new Custom();
 }

Tried to add $models->save() leaves the error Call to a member function save() on array
And tried $models[$index]->save(); but still
Was trying to get the error
if(!$model->save()){
Yii::warning($models[$index]->getErrors());
 }
But nothing came out. The queries are in the form
Custom[0][tovar]:Handle
Custom[0][number]:10
how to solve this problem?
June 26th 19 at 14:22
3 answers
June 26th 19 at 14:24
Solution
// create for each data set in the array model
$data = Yii::$app->request->post('Custom', []);
$models = [new Custom()];
foreach (array_keys($data) as $index) {
 $models[$index] = new Custom();
}
// load data from query to the array created models
if (Model::loadMultiple($models, Yii::$app->request->post())) {
 foreach ($models as $model) {
 //save the data
$model->save();
}
}
All I knew is that I didn't spend too much time there the same code. Thank you.
Rather fixated on other code which was not so important - Waylon_Kub commented on June 26th 19 at 14:27
June 26th 19 at 14:26
Call to a member function save() on array

Says that You call save() on the array, and the method save() is only for the objects AR. Apparently it is necessary to sort the array, there will be objects to which to apply the save method
I aprooval to do so $models[$index]->save(); but does not save but in the debug it gets this query
Custom[0][tovar]:Handle
Custom[0][number]:10
But until the end I do not understand how such a structure to iterate over - Waylon_Kub commented on June 26th 19 at 14:29
here is the query
Custom[0][tovar]:Handle

this is not a request
You gave only bits and pieces, so to say anything is difficult, but try:
foreach($models as $one){
$one->save();
}
- Waylon_Kub commented on June 26th 19 at 14:32
: It worked, can I make a suggestion in the future is to rewrite it in batchInsert for performance? - Henderson_Beat commented on June 26th 19 at 14:35
And yet I'm still faced with a problem, I kept 1 record, and now tried 2 record and again came the error Call to a member function save() on null
Although I was completely confused as to what maybe the problem, I used this design
foreach (array_keys($data) as $index) {
$models[$index]->save();
}
 }

Writes Call to a member function save() on null
Although this is what I get in debug
Custom[0][tovar]:Handle
Custom[0][number]:10
Custom[1][tovar]:test
Custom[1][number]:25
Not until the end I figured out how to save multiple value - Waylon_Kub commented on June 26th 19 at 14:38
It adds the first record only and all subsequent writes and adds - Waylon_Kub commented on June 26th 19 at 14:41
Because I threw a link to guide to do not write
https://github.com/hollandit/crm/blob/master/front... - Waylon_Kub commented on June 26th 19 at 14:44
June 26th 19 at 14:28
Tried as in the documentation to make mass assignment, but nothing helped and here is the option
Model::loadMultiple($models, Yii::$app->request->post())) {
 $data = Yii::$app->request->post('Custom', []);
 // foreach (array_keys($data) as $index) {
 // $models[$index]->save();
 // }
 Custom::getDb()->createCommand()->batchInsert('Custom', ['tovar', 'number'], $models);
 return $this->redirect(['shop']);
 }
- Waylon_Kub commented on June 26th 19 at 14:31
: You try to save a new model, but I do not understand why you look in the dock at the sample code for update: www.yiiframework.com/doc-2.0/guide-input-tabular-i...

Not to create: www.yiiframework.com/doc-2.0/guide-input-tabular-i...

upd: www.yiiframework.com/wiki/805/simple-way-to-implem... - Waylon_Kub commented on June 26th 19 at 14:34
To the account create I did, but I similarly I don't understand how what I'm doing wrong
$models = [new Custom()];
 $count = count(Yii::$app->request->post('Custom', []));
Model::loadMultiple($models, Yii::$app->request->post())) {
 for ($i = 0; $i < $count; $i++) {
 $models[$i] = new Custom();
$models[$i]->save();
}
 }
- Henderson_Beat commented on June 26th 19 at 14:37
And if you use $models[] = new Custom(); then I have error Cannot use [] for reading - Waylon_Kub commented on June 26th 19 at 14:40

Find more questions by tags Yii