Переделал в итоге на
Код: Выделить всё
/**
* Creates a new Car model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @throws \yii\web\HttpException
* @return mixed
*/
public function actionCreate()
{
if (Yii::$app->user->can('createCar')) {
$model = new Car;
$modelPhoto = new Photo;
if ($model->load(Yii::$app->request->post())) {
$images = UploadedFile::getInstances($modelPhoto, 'image');
$photos = [];
foreach ($images as $image) {
$modelPhoto = new Photo;
$modelPhoto->image = $image;
$photos[] = $modelPhoto;
}
$model->populateRelation('photos', $photos);
if ($model->save(false)) {
return $this->redirect(['view', 'id' => $model->id]);
}
} else {
$modelPhoto = new Photo();
return $this->render(
'create',
[
'model' => $model,
'modelPhoto' => $modelPhoto
]
);
}
} else {
throw new HttpException('403', Yii::t('app', 'У вас нет прав для публикации объявлений'));
}
}
/**
* Updates an existing Car model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @throws \yii\web\HttpException
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if (Yii::$app->user->can('updateCar', ['car' => $model])) {
$modelPhoto = new Photo;
$modelsPhoto = $model->photos;
if ($model->load(Yii::$app->request->post())) {
$images = UploadedFile::getInstances($modelPhoto, 'image');
$photos = [];
foreach ($images as $image) {
$modelPhoto = new Photo;
$modelPhoto->image = $image;
$photos[] = $modelPhoto;
}
$model->populateRelation('photos', $photos);
if ($model->save(false)) {
return $this->redirect(['view', 'id' => $model->id]);
}
} else {
return $this->render(
'update',
[
'model' => $model,
'modelsPhoto' => $modelsPhoto,
'modelPhoto' => $modelPhoto
]
);
}
} else {
throw new HttpException('403', Yii::t('app', 'У вас нет прав для редактирования данного объявления'));
}
}