In yii2 how to give alias to columns

241 Views Asked by At

I was looking way to give alias to column names in select, I have tried following

Model::find()->select(['columnName' => 'columnAlias'])

but this give error unknown columnName, if I swap them, no data is being displayed in results array, however it give correct no of rows

2

There are 2 best solutions below

0
Paresh Maheshwari On

I guess this should work.

Model::find()->select(['columnName as columnAlias']);

Also, here columnName should be replaced with the column you are trying to fetch, it's just for the info in case don't know it.

0
Muhammad Yusrizal On

You can do it like

Model::find()->select('columnName as columnAlias');

or

Model::find()->select(['columnAlias' => 'columnName'])