2009年12月18日星期五

Re: [fw-db] Zend_Db_Select Question

begin:vcard
fn:Mark T. Dame
n:Dame;Mark
org:MFM Software, Inc.
adr;dom:;;11988 Tram Way;Cincinnati;OH;45241
email;internet:mdame@mfm.com
title:VP, Product Development
tel;work:1-513-618-5232
url:http://www.mfm.com/
version:2.1
end:vcard

Mark T. Dame wrote:

Andre Fernandes wrote:
have you tried:

$select = $db->select()              ->from(array('z' => 'table_z'),                      array('col_a',                           'col_b',                                                     'col_c',                            'col_d' =>                               new Zend_Db_Expr('some value'))                     );

No, I hadn't tried that, but it didn't work.  It still wants to treat the second argument as a column name.

After a bit of debugging to see what query was actually being generated I discovered that this was the right track, but only part of the answer.

My original attempt:

  $select = $this->select();   $select->from($this->name, array('*', 'col_d' => '"Some Value"')); 

Results in a query like this:

  SELECT `table_z`.*,`table_z`.`Some Value` AS `col_d` FROM `table_z` ...  
Using the Zend_Db_Expr approach to the same query:

  $select = $this->select();   $select->from($this->name, array('*', 'col_d' => new Zend_Db_Expr("Some Value")));  
results in:

  SELECT `table_z`.*,Some Value AS `col_d` FROM `table_z` ...  
Which is really close.  The other half turns out to be quote():

  $select = $this->select();   $select->from($this->name, array('*', 'col_d' => new Zend_Db_Expr($db->quote("Some Value"))));  
results in:

  SELECT `table_z`.*,'Some Value' AS `col_d` FROM `table_z` ... 

There has to be a better way, but this at least works.


-m
--
MFM Software, Inc Mark T. Dame <mdame@mfm.com>
VP, Product Development
MFM Software, Inc. (http://www.mfm.com/)
"Where ever you go [for lunch], I'll take a number three."
     -- Duncan Ingraham

没有评论: