我想用Yii 2编写这样的SQL:
select id, 1 as type from user;
这是我的代码:
$query = User::find()->select(['id', '1 as type'])->all();
1
是一个常量,而不是用户的字段
我想在查询结果中添加字段type = 1
。
答案 0 :(得分:6)
要在查询的某些部分禁用引用和转义,请将其包装在yii\db\Expression中:
use yii\db\Expression;
...
$query = User::find()->select(['id', new Expresssion('1 as type')])->all();