Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent quoting of column name in SQL in Yii2

Tags:

sql

php

yii2

I want to write SQL like this with Yii 2:

select id, 1 as type from user;

This is my code:

$query = User::find()->select(['id', '1 as type'])->all();

1 is a constant, not user's field

I want to add field type = 1 to the query result.

like image 257
Mr.guX Avatar asked Jul 26 '16 03:07

Mr.guX


1 Answers

To disable quoting and escaping in certain part of the query, wrap it in yii\db\Expression:

use yii\db\Expression;

...

$query = User::find()->select(['id', new Expresssion('1 as type')])->all();
like image 139
arogachev Avatar answered Nov 12 '22 09:11

arogachev