Collection: addFieldToSelect vs select columns

Updated . Posted . Visible to the public.

collection->addFieldToSelect('col_name') will include the ID column in result. To return only columns of interest, use

$collection->getSelect()
    ->reset(Zend_Db_Select::COLUMNS)
    ->columns(['col_name']);

Example:

$collection->getSelect()
   ->reset(Zend_Db_Select::COLUMNS)
   ->columns(['entity_id' => 'mageproductid', 'value' => 'userid']);
$result = $collection->toArray();

$result output:

 array(2) {
  ["totalRecords"] => int(1565)
  ["items"] => array(1565) {
    [0] => array(2) {
      ["entity_id"] => string(2) "16"
      ["value"] => string(2) "24"
    }
    [1] => array(2) {
      ["entity_id"] => string(2) "17"
      ["value"] => string(2) "24"
    }
    [2] => array(2) {
      ["entity_id"] => string(2) "18"
      ["value"] => string(2) "24"
    }
  //...
kiatng
Last edit
kiatng
Posted by kiatng to OpenMage (2018-11-16 01:43)