Search code examples
zend-dbplaceholder

Zend_Db problem whith the placeholders from SQL string


I have the following SQL

SELECT i_id AS "entity_id", "entity_1" AS "type"
FROM tbl_extensions WHERE ext = 50

which returns me the result and an additional column "type" with the value "entity_1"

to gain the same with Zend_Db I've tried:

$db->fetchAll($db->select()
                 ->from('tbl_extensions',
                         array('entity_id' => 'i_id',
                               'type' => 'entity_1'))
                    ->where('ext = ?', 50)));

But I have the following error:

Message: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tbl_extensions.type' in 'field list'

It looks like Zend tries to find a column instead of creating it within the result.

Could anyone help me with it?


Solution

  • easiest solution would be to use Zend_Db_Expr.

    $db->fetchAll($db->select()
        ->from('tbl_extensions',
            array(
                'entity_id' => 'i_id',
                new Zend_Db_Expr('"entity_1" AS "type"'),
            )
        )
        ->where('ext = ?', 50));