2010年5月28日星期五

[fw-db] Statement containing both parentheses and AS clause

Example SELECT statement is like this:

SELECT CAST(column_name AS UNSIGNED)
    FROM table1

Code I used was:
  1. $select = $db->select()
  2.              ->from(array('table1'),
  3.                     array('CAST(column_name AS UNSIGNED)',
  4.                    );
which did not work - error returned, was: Could not find "table1.CAST(column_name" field. As we know, if there is a parentheses inside the clause, it is considered to be a Zend_Db_Expr. But on the other hand, the existence of "AS" clause messes things up, making it think we have a field-name-alias here, which we don't. Its just CAST function's syntax. Of course I overcame the problem by explicitly telling it we are having Zend_Db_Expr with this code which worked:

  1. $select = $db->select()
  2.              ->from(array('table1'),
  3.                     array('column_name' => new Zend_Db_Expr('CAST(column_name AS UNSIGNED)'))                 
  4.                 );
Question for developers of ZF is: Shouldn't you guys not touch any clauses, including AS clause, inside the statement which contains parentheses - the same way you do with Zend_Db_Expr objects? Statements with parentheses are considered to be Zend_Db_Expr objects, but at the same time they are not...

Shehi

没有评论: