public function actionRemoveAll() { $items = Yii::$app->request->post('items', []); if (!empty($items)) { $items = Discount::find()->where(['in', 'id', $items])->all(); foreach ($items as $item) { $item->delete(); } } return $this->redirect(['index']); }
/** * @return array|mixed */ private static function getAllDiscounts() { if (self::$allDiscounts === []) { $cacheKey = 'getAllDiscounts'; if (!(self::$allDiscounts = Yii::$app->cache->get($cacheKey))) { self::$allDiscounts = []; $discounts = Discount::find()->all(); foreach ($discounts as $discount) { self::$allDiscounts[$discount->appliance][] = $discount; } Yii::$app->cache->set($cacheKey, self::$allDiscounts, 86400, new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(Discount::className())]])); } } return self::$allDiscounts; }
/** * @param array $types * @return array */ protected static function getDiscountsByAppliance($types = []) { if (true === empty($types)) { return []; } if (null === static::$discountsByAppliance) { static::$discountsByAppliance = []; foreach (Discount::find()->all() as $model) { /** @var Discount $model */ static::$discountsByAppliance[$model->appliance][$model->id] = $model; } } $result = []; foreach ($types as $type) { if (true === isset(static::$discountsByAppliance[$type])) { $result = array_merge($result, static::$discountsByAppliance[$type]); } } return $result; }