Код: Выделить всё
public function actionCreate()
{
$productManager = new ProductManager();
if ($productManager->save()) {
return $this->redirect(['index']);
}
return $this->render('create', [
'product' => $productManager->getProduct(),
]);
}
public function actionUpdate($id)
{
$productManager = new ProductManager(['productId' => $id]);
if ($productManager->save()) {
return $this->redirect(['index']);
}
return $this->render('update', [
'product' => $productManager->getProduct(),
]);
}
public function actionDelete($id)
{
$productManager = new ProductManager(['productId' => $id]);
if ($productManager->delete()) {
return $this->redirect(['index']);
}
return $this->redirect(['index']);
}
Код: Выделить всё
public function save()
{
$product = $this->product;
$transaction = Yii::$app->db->beginTransaction();
try {
if (!$product->load(Yii::$app->request->post()) || !$product->save()) {
throw new ErrorException('Unable to save product.');
}
ProductPhoto::deleteAll([
'product_id' => $product->id,
'is_main' => true
]);
$photo = new ProductPhoto();
$photo->product_id = $product->id;
$photo->url = $product->mainPhotoUrl;
$photo->is_main = true;
if (!$photo->save()) {
throw new ErrorException('Unable to save attached photo.');
}
$transaction->commit();
} catch (\Exception $e) {
$transaction->rollBack();
return false;
}
return true;
}