Answer the question
In order to leave comments, you need to log in
Database entity statuses. What is the best way to do it?
I will try to explain on the fingers. Here is an example of a picture gallery model:
<?php
namespace app\models\galleries;
use Yii;
use app\models\galleries\GalleryImages;
class Gallery extends \yii\db\ActiveRecord
{
const STATUS_INACTIVE = 0; // неактивный / в пуле
const STATUS_ACTIVE = 10; // активный
const STATUS_PROCESS = 20; // в процессе обработки.
const STATUS_ERROR = 50; // ошибка.
const STATUS_IMPORTED = 80; // импортирован
const STATUS_DELETED = 90; // удален
public static function tableName()
{
return 'gallery';
}
public function rules()
{
return [
[['created_by', 'studio', 'mix', 'images_count', 'status'], 'integer'],
[['created_at', 'published_at'], 'safe'],
[['hash'], 'string', 'max' => 32],
[['title'], 'string', 'max' => 255],
];
}
public function attributeLabels()
{
return [
'g_id' => 'G ID',
'created_by' => 'Created By',
'hash' => 'Hash',
'title' => 'Title',
'studio' => 'Studio',
'mix' => 'Mix',
'images_count' => 'Images Count',
'published_at' => 'Published At',
'created_at' => 'Created At',
];
}
public function getHasImages()
{
return !empty($this->images);
}
public function getImages()
{
return $this->hasMany(GalleryImages::className(), ['g_id' => 'g_id']);
}
public static function getStatuses()
{
return [
self::STATUS_INACTIVE => 'Не активно',
self::STATUS_ACTIVE => 'Активно',
self::STATUS_PROCESS => 'В обработке',
self::STATUS_ERROR => 'Ошибка',
self::STATUS_IMPORTED => 'Импортировано',
self::STATUS_DELETED => 'Удалено',
];
}
public function getColoredStatus()
{
$statuses = [
self::STATUS_INACTIVE => '<span class="text-status brown">Не активно</span>',
self::STATUS_ACTIVE => '<span class="text-status green">Активно</span>',
self::STATUS_PROCESS => '<span class="text-status purple">В обработке</span>',
self::STATUS_ERROR => '<span class="text-status brown">Ошибка</span>',
self::STATUS_IMPORTED => '<span class="text-status orange">Импортировано</span>',
self::STATUS_DELETED => '<span class="text-status brown">Удалено</span>',
];
return isset($statuses[$this->status]) ? $statuses[$this->status] : 'unknown';
}
}
<?php
namespace app\models\galleries;
use Yii;
use app\models\galleries\Gallery;
class GalleryStatuses
{
public static $coloredStatuses = [
Gallery::STATUS_INACTIVE => '<span class="text-status brown">Не активно</span>',
Gallery::STATUS_ACTIVE => '<span class="text-status green">Активно</span>',
Gallery::STATUS_PROCESS => '<span class="text-status purple">В обработке</span>',
Gallery::STATUS_ERROR => '<span class="text-status brown">Ошибка</span>',
Gallery::STATUS_IMPORTED => '<span class="text-status orange">Импортировано</span>',
Gallery::STATUS_DELETED => '<span class="text-status brown">Удалено</span>',
];
public static function getColoredStatus(int $status) :string
{
return isset(self::$coloredStatuses[$status]) ? self::$coloredStatuses[$status] : 'unknown';
}
}
Answer the question
In order to leave comments, you need to log in
What makes you think it's cached?
Model data is cached, not all its functions with giblets. No caches would be enough to keep the same functions for each model...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question