-
-
----2018-11-09 22:07:36
Yii
----, 2018-11-09 22:07:36

Fixed values ​​for dropdown (more than 2) - in E-NUM or where?

Hello! The following question arose:
1) there are about 10 tables (of entities) ....
2) in each of them - everything is standard (CRUD, etc.)
3) in each table there are 2-3 fields, which consist of " more than two options".. To make it clear:
- there is a table Orders ..
- each Order has a status with options: live, error, new, canceled
I want to display in the GridView in the filter those statuses that are in the values ​​of the status
column when adding an Order and editing it, you already need to display ALL options...
While everything is done "as usual" - it is rigidly written in the views with "handles" .. but this is a "crutch"... I want to get rid of it

'filter' => Html::activeDropDownList($searchModel, 'status', ['active' => 'Active', 'live' => 'Live', 'error'=>'Error'], ['class' => 'form-control', 'prompt' => 'Все']),

Question: where "fix all possible options"?
In the model, of course, you can do something like this in the rules:
[
    // проверяет, что значение "level" равно Live, Active или Error
    ['level', 'in', 'range' => [Live, Active, Error]],
]

I understand correctly that this is a more or less safe way and the "foreign status" in the database will not work?
https://www.yiiframework.com/doc/guide/2.0/en/tuto...
But how then to pull out all these allowed options for display in the Edit / Add Form and filter GridView?
I don’t know...
Also, I thought, to put all available options in the E-NUM field "status" of the Orders table - but again, I don’t know how to pull out all available options...
Tell me the true way to solve this problem...
Please do not judge strictly - with OOP in general I am familiar recently .. with Yii - 3 weeks =)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-11-09
@stalkerxxl

Good evening.
I would do it a little differently.
In the model, I would declare constants, each of which would correspond to some status.
Do not use names, only numerical values. Make the field in the table under the status smallint(6), NULL is not present, the default is 0.
Something like this:

const STATUS_LIVE = 0;
const STATUS_ERROR = 1;
/* и так далее */

public function rules()
{
   return [
            ['status', 'integer'],
            ['status', 'default', 'value' => self::STATUS_LIVE],
            ['status', 'in', 'range' => array_keys(self::getStatusesArray())],
   ];
}

/* метод использовать при выводе статуса в view.php*/
public function getStatusName()
{
   return ArrayHelper::getValue(self::statusesArray(), $this->status);
}

public static function getStatusesArray()
{
     return [
        self::STATUS_LIVE => 'Live',
       self::STATUS_ERROR => 'Error',
       /* и так далее */
    ];
}

In gridView
In dropDown form
$form->field($model, 'status')->dropDownListYOUR_MODEL_NAME::getStatusesArray(), ['prompt' => 'Select'])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question