protected function findModel($id) { if (($model = FilterVariant::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function beforeDelete() { foreach ($this->hasMany(FieldRelationValue::className(), ['filter_id' => 'id'])->all() as $frv) { $frv->delete(); } foreach ($this->hasMany(FilterVariant::className(), ['filter_id' => 'id'])->all() as $fv) { $fv->delete(); } return true; }
public function search($params) { $query = FilterVariant::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => false]); $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'filter_id' => $this->filter_id]); $query->andFilterWhere(['like', 'value', $this->value]); return $dataProvider; }
public function filtered($filterIds = false, $mode = 0) { if (!$filterIds) { $filterIds = Yii::$app->request->get($this->fieldName); } if (empty($filterIds)) { return $this->owner; } $condition = ['OR']; $variantCount = 0; $filterCount = count($filterIds); foreach ($filterIds as $filterId => $value) { $filter = Filter::findOne($filterId); if ($filter->type == 'range' && is_string($value)) { $value = explode(';', $value); if ($value[0] != $value[1]) { $variants = FilterVariant::find()->where('filter_id = :filterId AND (numeric_value >= :min AND numeric_value <= :max)', [':filterId' => $filterId, ':min' => $value[0], ':max' => $value[1]])->select('id')->all(); } else { $variants = FilterVariant::find()->where('filter_id = :filterId AND numeric_value = :value', [':filterId' => $filterId, ':value' => $value[0]])->select('id')->all(); } $variantIds = ArrayHelper::map($variants, 'id', 'id'); } else { $variantIds = $value; } $condition[] = ['filter_id' => $filterId, 'variant_id' => $variantIds]; if ($mode == 1) { $variantCount += count($variantIds); } else { $variantCount++; } } $filtered = FilterValue::find()->select('item_id')->groupBy('item_id')->andHaving("COUNT(DISTINCT `filter_id`) = {$variantCount}")->andFilterWhere($condition); if ($filtered->count() > 0) { $this->owner->andWhere(['id' => $filtered]); } else { $this->owner->andWhere(['id' => 0]); } return $this->owner; }
public static function saveEdit($id, $name, $value) { $setting = FilterVariant::findOne($id); $setting->{$name} = $value; $setting->save(); }
public function getVariant() { return $this->hasOne(FilterVariant::className(), ['id' => 'variant_id']); }
public function actionEditVariant() { $name = yii::$app->request->post('name'); $value = yii::$app->request->post('value'); $pk = unserialize(base64_decode(yii::$app->request->post('pk'))); FilterVariant::saveEdit($pk, $name, $value); }