public function actionCreate()
{
$models = [new Item()];
$request = Yii::$app->getRequest();
if ($request->isPost && $request->post('ajax') !== null) {
$data = Yii::$app->request->post('Item');
foreach (array_keys($data) as $index) {
$models[$index] = new Item();
}
Model::loadMultiple($models, Yii::$app->request->post());
Yii::$app->response->format = Response::FORMAT_JSON;
}
if (Model::loadMultiple($models, Yii::$app->request->post()) && Model::validateMultiple($models)){
foreach($models as $model){
if(!$model->save()){
debug($model->getErrors());
}
}
Yii::$app->session->setFlash('success', "Данные добавлены");
return $this->redirect('index');
}
return $this->render('create', [
'models' => $models,
]);
}
Array
(
[_csrf-frontend] => ylh35gb_PlqO2eXNtnthe-UG2kEUx9t8Uc6Q1r5sOLONCDW5Vo16bOOTvaX5DRQa1my_GCKQ4wQcpsGk6h1N_w==
[Item] => Array
(
[0] => Array
(
[id] =>
[title] => Test 1
[description] => TD1
)
[1] => Array
(
[id] =>
[title] => Test 2
[description] => TD2
)
[2] => Array
(
[id] =>
[title] => Test 3
[description] => TD3
)
[3] => Array
(
[id] =>
[title] => Test 4
[description] => TD4
)
)
)
$models = [new Item()];
$models = $this->getItems();
private function getItems()
{
$data = [
[
'id' => 1,
'title' => 'Title 11',
'description' => 'Description 1'
],
[
'id' => 2,
'title' => 'Title 22',
'description' => 'Description 2'
],
[
'id' => 3,
'title' => 'Title 33',
'description' => 'Description 3'
],
];
$items = [];
foreach ($data as $row) {
$item = new Item();
$item->setAttributes($row);
$items[] = $item;
}
return $items;
}
Теперь url такой стал..