Answer the question
In order to leave comments, you need to log in
How to replace values from a database in a view?
I am rewriting a Yii1 application to Yii2 for myself (studying).
It is not possible to replace numeric values from the database with text values in the view.
Yii1
view:
array(
'header' => 'Стоянка',
'value' => function($data)
{
$res = !empty($data->transport_storage)?(Transport::upload($data->transport_storage->storage_id)):"";
return $res;
},
),
public static function upload($storageId) {
$storage[2] = 'Парковка №1';
$storage[3] = 'Парковка №2';
$storage[4] = 'Парковка №3';
$storage[5] = 'Парковка №4';
return $storage[$storageId];
}
Answer the question
In order to leave comments, you need to log in
If I understand correctly, then there should be a getter in the model:
public static function parkingList() {
return [
1 => 'Парковка №1',
2 => 'Парковка №2';
];
}
public function getParkingName(){
$list = self::parkingList();
return (isset($this->transport_storage) and isset($list[$this->transport_storage->storage_id])) ? $list[$this->transport_storage->storage_id] : 'не заданно';
}
[
'header' => 'Стоянка',
'value' => 'parkingName'
]
Good afternoon.
It is not entirely clear where exactly your data is stored.
Are you trying to infer from a connected model or not?
Actually, this is how it's done:
'value' => 'name'; // если это родная модель
// or
'value' => 'model->related->name'; // если нужно получить данные через связь hasOne()
[
'attribute' => 'name_attribute',
'value' => function($model){
return Transport::upload()....;
}
]
public static function upload($storageId) {
$storage[2] = 'Парковка №1';
$storage[3] = 'Парковка №2';
$storage[4] = 'Парковка №3';
$storage[5] = 'Парковка №4';
return $storage[$storageId];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question