public function actionView($id){
$id = Yii::$app->request->get('id');
$product = Product::find()->where(['id' => $id])->one();
if(empty($product)){
throw new HttpException(404, 'Такого продукта нет');
}
return $this->render('view', compact('product'));
}
class ProductSize extends ActiveRecord
{
public static function tableName()
{
return 'product_size';
}
public function rules()
{
return [
[['product_id', 'size_id'], 'integer'],
[['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'id']],
[['size_id'], 'exist', 'skipOnError' => true, 'targetClass' => Size::className(), 'targetAttribute' => ['size_id' => 'id']],
];
}
public function getSize()
{
return $this->hasOne(Size::className(), ['id' => 'size_id']);
}
}
public function actionView(){
$session = Yii::$app->session;
$session->open();
$this->setMeta('Корзина');
$order = new Order();
if($order->load(Yii::$app->request->post())){
$order->qty = $session['cart.qty'];
$order->sum = $session['cart.sum'];
if($order->save()){
$this->saveOrderItems($session['cart'], $order->id);
$session->remove('cart');
$session->remove('cart.qty');
$session->remove('cart.sum');
return $this->redirect(['/cart/success', 'id'=>$order->id]);
}
else{
Yii::$app->session->setFlash('Error', 'Ошибка');
}
}
return $this->render('view', compact('session', 'order'));
}
protected function saveOrderItems($items, $order_id){
foreach ($items as $id=> $item){
$order_items = new OrderItems();
$order_items->order_id = $order_id;
$order_items->product_id = $id;
$order_items->name = $item['name'];
$order_items->price = $item['price'];
$order_items->qty_item = $item['qty'];
$order_items->size_id = $item['size_id'];
$order_items->sum_item = $item['price'] * $item['qty'];
debug($order_items);
$order_items->save();
}
}
foreach ($items as $id=> $item){
$order_items = new OrderItems();
$order_items->order_id = $order_id;
$order_items->product_id = $id;
$order_items->name = $item['name'];
$order_items->price = $item['price'];
$order_items->qty_item = $item['qty'];
$order_items->size_id = ;
$order_items->sum_item = $item['price'] * $item['qty'];
debug($order_items);
$order_items->save();
}
public function actionUpdate($id)
{
// $model = $this->findModel($id);
$model = Product::find()->where(['id' => $id])->with('productSize')->one();
$sizes = Size::find()->all();
$product_sizes = ArrayHelper::map(ProductSize::find()->where(['product_id'=> $model->id])->all(), 'size_id', 'size_id');
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$model->image = UploadedFile::getInstance($model, 'image');
if( $model->image ){
$model->upload();
}
unset ($model->image);
$model->gallery = UploadedFile::getInstances($model, 'gallery');
$model->uploadGallery();
Yii::$app->session->setFlash('success', "Товар {$model->name} обновлен");
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
'sizes' => $sizes,
'product_sizes' => $product_sizes
]);
}
}