/** * Add cross sale action * @throws \yii\db\Exception */ public function actionAddCrossSale() { $errors = false; $transaction = Product::getDb()->beginTransaction(); $request = Yii::$app->request; $discount = new Discount(['scenario' => 'insert']); if (!$discount->save()) { $errors = true; } $crossSelling = new CrossSelling(['scenario' => 'insert']); $crossSelling->discount_id = $discount->id; $crossSelling->variant_id_1 = $request->post('variant1'); $crossSelling->variant_id_2 = $request->post('variant2'); if (!$crossSelling->save()) { $errors = true; } if ($errors) { var_dump($crossSelling->errors, $discount->errors); $transaction->rollBack(); } else { $transaction->commit(); TagDependency::invalidate(Yii::$app->commonCache, Product::generateTagStatic($request->get('id'))); TagDependency::invalidate(Yii::$app->commonCache, Product::generateTagStatic($request->get('id'))); } }
/** * @return \yii\db\ActiveQuery */ public function getCrossSellings() { return $this->hasMany(CrossSelling::className(), ['discount_id' => 'id']); }