Answer the question
In order to leave comments, you need to log in
How to save data to database?
I use the yii2-multiple-input widget, I don’t understand a little in the documentation how to save data in the database with multiple values
in the guide if you need everything to view the model and the whole picture
Implemented like this 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();
}
if(!$model->save()){
Yii::warning($models[$index]->getErrors());
}
But it didn't work either. Requests come in the form Answer the question
In order to leave comments, you need to log in
// создаем на каждый набор данных в массиве модель
$data = Yii::$app->request->post('Custom', []);
$models = [new Custom()];
foreach (array_keys($data) as $index) {
$models[$index] = new Custom();
}
// загружаем данные из запроса в массив созданных моделей
if (Model::loadMultiple($models, Yii::$app->request->post())) {
foreach ($models as $model) {
//сохраняем данные
$model->save();
}
}
Call to a member function save() on array
yiiframework.domain-na.me/doc/guide/2.0/en/structu...
yiiframework.domain-na.me/doc/guide/2.0/en/db-acti...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question