Reputation: 61
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
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