Zend DB Selecting constants - columns that do not exist in table
Asked Answered
P

3

11

I'm trying to do this query using Zend DB select but I'm not able to do so

This is the sql query

select shopping_id,shopping_details,"friend" as type
from shopping

Notice here how I'm specifying "friend" as type and friend is not a column in the shopping table.

Now how do I do this in Zend. I have tried this but it gives me an error saying "sh.friend Column does not exist"

$select->from(array('sh'=>'shopping'),array('shopping_id','shopping_details','"friend" as type');

Any help will be appreciated thanks

Phantasmagoria answered 22/7, 2010 at 2:56 Comment(0)
T
20

Try with Zend_Db_Expr, maybe something like:

$select->from(array('sh'=>'shopping'),
    array('shopping_id','shopping_details',
         new Zend_Db_Expr('"friend" as type'));
Tokoloshe answered 22/7, 2010 at 6:24 Comment(1)
Worked for me too. ThanksArduous
S
2
$select->from(
    array('sh'=>'shopping'),
    array('shopping_id','shopping_details','friend'=>'type', 'alias'=>'column or expression')
);
Suttles answered 19/5, 2011 at 21:44 Comment(0)
C
0

For Zend Framework 2/3 or Laminas you have to use Laminas\Db\Sql\Expression. Make sure to quote your constant with a double-quote "".

$select->from(['e' => 'experience'])
    ->columns([
        'id' => 'id',
        'value' => 'title',
        'name' => new Laminas\Db\Sql\Expression('"skill"')
    ]);

*for Zend Framework the name of the expression class is Zend\Db\Sql\Expression.

Casein answered 2/11, 2021 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.