Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting arbitrary strings with Zend DB Select?

I am using the fluent interface to create a Zend DB Select object/query. As part of the query, I would like to select an arbitrary string, like "SELECT 'foo' AS 'type' FROM ...". foo is not a column, it's just a string literal.

When I select an arbitrary number, the query works as expected. When I change it to a string, Zend tries to treat foo as a column, and throws an error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'l.foo' in 'field list'

I have tried wrapping the string in Zend_Db_Expr in various ways such as:

$select->columns(array('type' => new Zend_Db_Expr('foo')));

That stops Zend from adding the correlation name, but it still treats it as a column:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'foo' in 'field list'

I feel like I must be missing something obvious here. How do I tell Zend to stop treating this as a column?

like image 848
wizzard Avatar asked May 03 '10 23:05

wizzard


1 Answers

Did you perhaps try:

$select->columns(array('type' => new Zend_Db_Expr("'foo'")));

You need to actually have quotes around the 'foo' in the SQL as well.

like image 74
gnarf Avatar answered Sep 19 '22 18:09

gnarf