Selecting arbitrary strings with Zend DB Select?
Asked Answered
F

1

11

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?

Fransen answered 3/5, 2010 at 23:18 Comment(0)
C
12

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.

Charleycharlie answered 3/5, 2010 at 23:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.