A
A
Andrey2018-02-27 13:43:08
Yii
Andrey, 2018-02-27 13:43:08

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;
      },
        ),

Model:
public static function upload($storageId) {
  $storage[2] = 'Парковка №1';
  $storage[3] = 'Парковка №2';
  $storage[4] = 'Парковка №3';
  $storage[5] = 'Парковка №4';
  return $storage[$storageId];
    }

How to remake this in Yii2 and is there a better way to write it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2018-02-27
@DronTat

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] : 'не заданно';
}

respectively in the view will be
[
            'header' => 'Стоянка',
            'value' => 'parkingName'
]

D
Dmitry, 2018-02-27
@slo_nik

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()

The output in DerailView is like this
[
   'attribute' => 'name_attribute',
   'value' => function($model){
       return Transport::upload()....;
   }
]

With this array, too, little is known.
public static function upload($storageId) {
  $storage[2] = 'Парковка №1';
  $storage[3] = 'Парковка №2';
  $storage[4] = 'Парковка №3';
  $storage[5] = 'Парковка №4';
  return $storage[$storageId];
    }

Do you have a number that needs to be converted to a string (roughly speaking)?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question