views:

251

answers:

2

I am trying to create a select statement that uses the following structure:

$db
    ->select()  
    ->from(  
        array('i' => ...),  
        array('provisional', 'itemID', 'orderID'))  
    ->columns(array("'0' AS provisionalQty", "'ballast' AS productType"))  
    ->joinLeft(  
        array('o' => ...),  
        'i.orderID = o.orderID', array())  
    ->joinLeft(  
        array('f' => ...),  
        'i.productID = f.fixtureID AND f.supplierID = o.supplierID', array())  
    ->joinLeft(  
        array('b' => ...),  
        'f.lampTechnology = b.lampTechnology ' .  
        ' AND f.lampCount = b.lampCount ' .  
        ' AND f.ballastVoltage = b.ballastVoltage ' .  
        ' AND b.supplierID = o.supplierID')  
    ->where('i.orderID = ?', $oObj->orderID, Zend_Db::INT_TYPE)  
    ->where('!i.hidden AND i.productType = ? AND !i.provisional', 'fixture')  

The equivalent in MySQL would look something like this (which works fine)...

SELECT '0' AS provisionalQty, 'ballast' AS productType, i.* FROM ... LEFT JOIN ... WHERE ...;

This, however, does not work as expected. The $db->columns() method expects there to be a table attached to each column even the 'pseudo'-columns. Any ideas?

-Chris

A: 

I think you're using column select incorrectly here. It should be something like:

// "ballast AS productType, 0 as provisionalQty"
$db->columns(array("productType" => "ballast", "provisionalQty" => 0));

Just remember the columns abstraction is reverse of the normal AS statement and you shouldn't have to include "AS."

Typeoneerror
Thanks for the response, but that was a no go.Fortunately, I just figured out a way to do this!
webjawns.com
Ah, sorry, that should've been an array. That's the syntax. Check the manual "Adding Expression Columns" http://framework.zend.com/manual/en/zend.db.select.html
Typeoneerror
I understand the syntax of Zend_Db_Select, I just didn't quite understand how to include values that were not part of any table. Zend_Db attaches the table name as a prefix to the values within the from() and columns() methods by default. That was where the problem was... Zend_Db_Expr allows the use of a statement without attaching the table name. Zend_Db_Expr seems to be the way to go.
webjawns.com
A: 

Because of the way this class works, it seems that using a string/array looks for a specific table, whereas the Zend_Db_Expr class does not require an actual table.

$db
    ->select()
    ->columns(new Zend_Db_Expr("'0' AS provisionalQty, 'ballast' AS productType"))
webjawns.com
You can also do it this way: ->columns(array('provisionalQty' => new Zend_Db_Expr('0'), 'productType' => 'ballast'));
David Caunt