예제 #1
0
 protected static function initCache($constrain, $key = null)
 {
     if (!\nitm\helpers\Cache::exists($key)) {
         $class = static::className();
         $model = new $class(['initSearchClass' => false]);
         $model->setConstraints($constrain);
         $key = is_null($key) ? \nitm\helpers\Cache::cacheKey($model, ['parent_id', 'parent_type']) : array_keys($constrain);
         \nitm\helpers\Cache::setModel($key, [$model->className(), \yii\helpers\ArrayHelper::toArray($model)]);
     } else {
         $array = \nitm\helpers\Cache::getModel($key);
         $model = new $array[0]($array[1]);
     }
     return $model;
 }
예제 #2
0
 public function getListViaClass($className = null)
 {
     if (!is_null($className) && class_exists($class = strpos($className, 'Category') !== false ? $className : $className . 'Category')) {
         $type = (new $class())->isWhat();
     } else {
         if (isset($this) && get_class($this) == __CLASS__) {
             $class = __CLASS__;
             $type = $this->isWhat();
         } else {
             $class = __CLASS__;
             $type = $class::isWhat();
         }
     }
     $key = 'list-' . md5($class);
     if (Cache::cache()->exists($key)) {
         return Cache::cache()->get($key);
     } else {
         $ret_val = parent::getList(null, ' ', ['orderBy' => ['name' => SORT_ASC], 'where' => ['type_id' => new \yii\db\Expression("(SELECT id FROM " . $class::tableName() . " WHERE slug='" . $type . "' LIMIT 1)")]]);
         Cache::cache()->set($key, $ret_val);
         return $ret_val;
     }
 }