| @@ 247-278 (lines=32) @@ | ||
| 244 | * @param int|null $isActive |
|
| 245 | * @return Category|null |
|
| 246 | */ |
|
| 247 | public static function findById($id, $isActive = 1) |
|
| 248 | { |
|
| 249 | if (!is_numeric($id)) { |
|
| 250 | return null; |
|
| 251 | } |
|
| 252 | if (!isset(static::$identity_map[$id])) { |
|
| 253 | $cacheKey = static::tableName() . ":$id:$isActive"; |
|
| 254 | if (false === $model = Yii::$app->cache->get($cacheKey)) { |
|
| 255 | $model = static::find()->where(['id' => $id])->with('images'); |
|
| 256 | if (null !== $isActive) { |
|
| 257 | $model->andWhere(['active' => $isActive]); |
|
| 258 | } |
|
| 259 | if (null !== $model = $model->one()) { |
|
| 260 | Yii::$app->cache->set( |
|
| 261 | $cacheKey, |
|
| 262 | $model, |
|
| 263 | 86400, |
|
| 264 | new TagDependency( |
|
| 265 | [ |
|
| 266 | 'tags' => [ |
|
| 267 | ActiveRecordHelper::getObjectTag($model, $model->id) |
|
| 268 | ] |
|
| 269 | ] |
|
| 270 | ) |
|
| 271 | ); |
|
| 272 | } |
|
| 273 | } |
|
| 274 | static::$identity_map[$id] = $model; |
|
| 275 | } |
|
| 276 | ||
| 277 | return static::$identity_map[$id]; |
|
| 278 | } |
|
| 279 | ||
| 280 | /** |
|
| 281 | * Finds category by slug inside category_group_id |
|
| @@ 253-284 (lines=32) @@ | ||
| 250 | * @param int $isActive Return only active |
|
| 251 | * @return Product |
|
| 252 | */ |
|
| 253 | public static function findById($id, $isActive = 1) |
|
| 254 | { |
|
| 255 | if (!is_numeric($id)) { |
|
| 256 | return null; |
|
| 257 | } |
|
| 258 | if (!isset(static::$identity_map[$id])) { |
|
| 259 | $cacheKey = static::tableName() . ":$id:$isActive"; |
|
| 260 | if (false === $model = Yii::$app->cache->get($cacheKey)) { |
|
| 261 | $model = static::find()->where(['id' => $id])->with('images'); |
|
| 262 | if (null !== $isActive) { |
|
| 263 | $model->andWhere(['active' => $isActive]); |
|
| 264 | } |
|
| 265 | if (null !== $model = $model->one()) { |
|
| 266 | /** |
|
| 267 | * @var self $model |
|
| 268 | */ |
|
| 269 | static::$slug_to_id[$model->slug] = $id; |
|
| 270 | Yii::$app->cache->set( |
|
| 271 | $cacheKey, |
|
| 272 | $model, |
|
| 273 | 86400, |
|
| 274 | new TagDependency( |
|
| 275 | [ |
|
| 276 | 'tags' => [ |
|
| 277 | ActiveRecordHelper::getCommonTag(static::className()) |
|
| 278 | ] |
|
| 279 | ] |
|
| 280 | ) |
|
| 281 | ); |
|
| 282 | } |
|
| 283 | } |
|
| 284 | static::$identity_map[$id] = $model; |
|
| 285 | } |
|
| 286 | return static::$identity_map[$id]; |
|
| 287 | } |
|