0
 $collection = $this->productCollectionFactory->create();
 $collection->addAttributeToSelect('*');
  • collection->getData() -> returns only a few data (most of the main table until explicitly provided in addAttributeToSelect('my_attr_code').
  • collection->toArray() -> returns complete data.

This behaviour is same on the Flat tables, but why different on EAV table such as product?

1 Answer 1

1
Eav collection: $collection->addAttributeToSelect('*'); 
Flat collection: $collection->addFieldToSelect('*'); 

Collections of flat tables are inherited from:

Magento\Framework\Data\Collection

   /**
     * Convert collection to array
     */
    public function toArray($arrRequiredFields = [])
    {
        $arrItems = [];
        $arrItems['totalRecords'] = $this->getSize();

        $arrItems['items'] = [];
        foreach ($this as $item) {
            $arrItems['items'][] = $item->toArray($arrRequiredFields);
        }
        return $arrItems;
    }

Collection of EAV entities have abstract layer from:

Magento\Eav\Model\Entity\Collection\AbstractCollection Magento\Framework\Data\Collection\AbstractDb

   /**
     * Retrieve array of attributes
     */
    public function toArray($arrAttributes = [])
    {
        $arr = [];
        foreach ($this->getItems() as $key => $item) {
            $arr[$key] = $item->toArray($arrAttributes);
        }
        return $arr;
    }


    /**
     * Retrieve collection items
     *
     * @return \Magento\Framework\DataObject[]
     */
    public function getItems()
    {
        $this->load();
        return $this->_items;
    }

    /**
     * Load data
     *
     */
    public function load($printQuery = false, $logQuery = false)
    {
        return $this->loadData($printQuery, $logQuery);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.