Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zend not using columns, and selecting everything

I have the following code

$result = $handle->select()->from('store_details')
                               ->where('store_details.store_id=?', $id)
                               ->columns('store_details.store_name');
                               //->query(ZEND_DB::FETCH_OBJ);

However, when I run it, the entire row is selected, not just the column I wanted. Here is the output from __toString

SELECT `store_details`.*, `store_details`.`store_name` 
FROM `store_details` WHERE (store_details.store_id=8)

Any help?

like image 472
daniel Avatar asked Jun 21 '09 12:06

daniel


2 Answers

The columns() method is for adding columns to an existing from or join. The correct way to build your query is:

$result = $handle->select()->from('store_details','store_details.store_name')->where('store_details.store_id=?', $id);

You need to specify the columns you want as the second parameter to the from() method, as a string if it is just one column, or as an array for multiple columns. From the Zend_Db_Select docs:

In the second argument of the from() method, you can specify the columns to select from the respective table. If you specify no columns, the default is "*", the SQL wildcard for "all columns".

You can list the columns in a simple array of strings, or as an associative mapping of column alias to column name. If you only have one column to query, and you don't need to specify a column alias, you can list it as a plain string instead of an array.

like image 198
karim79 Avatar answered Nov 14 '22 06:11

karim79


If you already have the select object (means the from() was called before), you should use $select->reset(Zend_Db_Select::COLUMNS); and then call columns() as you do in the example.

like image 38
Tomáš Fejfar Avatar answered Nov 14 '22 06:11

Tomáš Fejfar