A
A
AlikDex2016-10-14 21:48:49
Yii
AlikDex, 2016-10-14 21:48:49

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:

spoiler

<?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';
    }
}


I once looked at all sorts of guides there, they do something like this (with various variations). But then something dawned. After all, if these models are cached, then this list of statuses is stored in each one is superfluous. The question arose, where to take them out in this case is better? At first I thought somehow in lang. arrays to store lists, but did not find how. Then the idea came to transfer these statuses to the form. Well, we kind of call the form, where these lists are usually required completely for selects. The idea seemed silly also because there can be several forms. It can’t even be more precise, but there are usually several of them. And in each status, storing is also not ice, especially if something needs to be changed or added.
Here.
Another thought came. Can generally get a separate class for such purposes? Which will return the necessary static methods. Something like:
<?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';
    }
}

What do you think about this? How do people generally act in such cases (I mean not those that are written by guides)?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Alexander Aksentiev, 2016-10-15
@Sanasol

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 question

Ask a Question

731 491 924 answers to any question