/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = ProductModel::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'price' => $this->price, 'quantity' => $this->quantity, 'isAvailable' => $this->isAvailable, 'rating' => $this->rating, 'amountRated' => $this->amountRated, 'idCategory' => $this->idCategory]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'description', $this->description])->andFilterWhere(['like', 'photo', $this->photo]); return $dataProvider; }
public function actionAdd() { $model = new CommentModel(); $model->date = date("Y-m-d"); if ($model->load(Yii::$app->request->post()) && $model->save()) { $product = ProductModel::find()->where(['id' => $model->idProduct])->one(); $product->rating = ($product->rating * $product->amountRated + $model->mark) / ($product->amountRated + 1); $product->amountRated = $product->amountRated + 1; if (!$product->save()) { echo "Error updating product."; } } else { echo "Error adding comment."; } return $this->redirect(\Yii::$app->user->getReturnUrl()); }
use app\models\CharacteristicModel; use app\models\ProductModel; use yii\helpers\ArrayHelper; /* @var $this yii\web\View */ /* @var $model app\models\CharacteristicValueModel */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="characteristic-value-model-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'idProduct')->dropDownList(ArrayHelper::map(ProductModel::find()->all(), 'id', 'name')); ?> <?php echo $form->field($model, 'idCharacteristic')->dropDownList(ArrayHelper::map(CharacteristicModel::find()->all(), 'id', 'name')); ?> <?php echo $form->field($model, 'value')->textInput(['maxlength' => 50]); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div>
public function getDelete($id) { $product = \App\Models\ProductModel::find($id); if ($product) { $product->delete(); } }
/** * Deletes an existing CommentModel model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $model = $this->findModel($id); $product = ProductModel::find()->where(['id' => $model->idProduct])->one(); if ($product->amountRated > 1) { $product->rating = ($product->rating * $product->amountRated - $model->mark) / ($product->amountRated - 1); } else { $product->rating = 0; } $product->amountRated = $product->amountRated - 1; $product->save(); $model->delete(); return $this->redirect(['index']); }
public static function getProductsInCategories($categoryIds) { $result_products = array(); foreach ($categoryIds as $categoryId) { $products = \Yii::$app->cache->get('products_category_' . $categoryId); if ($products === false) { $products = ProductModel::find()->where(['idCategory' => $categoryId])->all(); \Yii::$app->cache->add('products_category_' . $categoryId, $products); } foreach ($products as $product) { $result_products[] = $product; } } return $result_products; }