Zend selects all columns
Asked Answered
S

2

7

In the following code:


$selectColumns= array('user_id.user_email', // inner join the data from user_id and user_details
                            'user_details.first_name', 
                            'user_details.last_name');
        $result = $handle->select()->from('user_id', $selectColumns)
                                   ->where('user_id.uid=?', $uid)
                                   ->join('user_details', 'user_id.uid = user_details.uid')
                                   ->query(ZEND_DB::FETCH_OBJ);

Zend selects all the columns in the table, not just the requested ones.

How can I select only some?

Sarcoid answered 23/6, 2009 at 16:54 Comment(0)
C
8

The problem is in your join() method call:

->join('user_details', 'user_id.uid = user_details.uid')

The optional third argument is columns from this table. If the argument is absent, it defaults to user_details.*.

Note that you added qualified columns from both tables in the from() table, but this has no effect on the default of user_details.*. Sorry, but Zend_Db_Select just isn't smart enough to keep track of all that.

You can make the join() call add no columns by passing an empty array:

->join('user_details', 'user_id.uid = user_details.uid', array())

The qualified columns you added in the from() call should still be there. To verify this yourself, print the SQL:

print $result . "\n"; // calls __toString() method on Zend_Db_Select object
Counterpane answered 23/6, 2009 at 17:1 Comment(0)
M
6

Add another parameter to your join at the end -- an empty array. That will tell it to select no columns from the join. With the code you have now, you are selecting all columns from the joined table.

->join('user_details', 'user_id.uid = user_details.uid', array())
Millham answered 23/6, 2009 at 17:1 Comment(1)
+1 for getting there first, +1 for Bill for the extra detail. Why do only Zend people vote Zend people up?Fritts

© 2022 - 2024 — McMap. All rights reserved.