zend not using columns, and selecting everything
Asked Answered
O

2

7

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?

Oreopithecus answered 21/6, 2009 at 12:28 Comment(0)
W
10

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.

Wabash answered 21/6, 2009 at 12:30 Comment(1)
This is good to know (!) since the documentation doesn´t mention the part about adding columns: "Specifies the columns used in the FROM clause."Cacilia
C
4

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.

Clotho answered 21/6, 2009 at 22:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.