Mr.guX
Mr.guX

Reputation: 61

How to prevent quoting of column name in SQL in 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.

Upvotes: 6

Views: 3577

Answers (1)

arogachev
arogachev

Reputation: 33548

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 Expression('1 as type')])->all();

Upvotes: 12

Related Questions