Yii2: Virtual column not works in QueryBuilder

Created on 5 Aug 2014  Â·  6Comments  Â·  Source: yiisoft/yii2

I am trying execute the query below:

 (new Query())->select("id, 'post' as type, name")->from("post")->all();

But occurs this exception:

SQLSTATE[42S22]: Column not found: 1054 Unknown column ''post'' in 'field list'
The SQL being executed was: SELECT `id`, `'post'` AS `type`, `name` FROM `post`
1. in D:\fontes\php\yii2-basic\vendor\yiisoft\yii2\db\Schema.php at line 520
2. in D:\fontes\php\yii2-basic\vendor\yiisoft\yii2\db\Command.php – yii\db\Schema::convertException(PDOException, 'SELECT `id`, `'post'` AS `type`,...') at line 855
3. in D:\fontes\php\yii2-basic\vendor\yiisoft\yii2\db\Command.php – yii\db\Command::queryInternal('fetchAll', null) at line 350
4. in D:\fontes\php\yii2-basic\vendor\yiisoft\yii2\db\Query.php – yii\db\Command::queryAll() at line 204
5. in D:\fontes\php\yii2-basic\controllers\SiteController.php – yii\db\Query::all()
docs

Most helpful comment

yes it's the same.
This way works:

(new Query())->select(['id', new Expression('NULL as type'), 'name'])->from("post")->all();

it's right?

All 6 comments

yes, because there is no column with quotes, remove them from select part of post

just try it, example may be incorrect (i think it was copy - paste mistake), you also can try array as input for select to simplify parsing. There are also similar examples in same docs above as you may noted, that dont use quotes

I've tried, same error

can you show exception? is it same as above?

yes it's the same.
This way works:

(new Query())->select(['id', new Expression('NULL as type'), 'name'])->from("post")->all();

it's right?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

newscloud picture newscloud  Â·  3Comments

psfpro picture psfpro  Â·  3Comments

indicalabs picture indicalabs  Â·  3Comments

SamMousa picture SamMousa  Â·  3Comments

skcn022 picture skcn022  Â·  3Comments