Zend selects all columns

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 choose only some?

+7
php zend-framework zend-db
source share
2 answers

The problem is calling the join() method:

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

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

Please note that you have added qualified columns from both tables to the from() table, but this does not affect the default value of user_details.* . Sorry, but Zend_Db_Select simply not smart enough to keep track of all this.

You can make a join() call not to add columns by passing an empty array:

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

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

 print $result . "\n"; // calls __toString() method on Zend_Db_Select object 
+6
source share

Add another parameter to your connection at the end - an empty array. This will tell him not to select columns from the join. With the code that you have now, you select all the columns from the joined table.

 ->join('user_details', 'user_id.uid = user_details.uid', array()) 
+6
source share

All Articles