5

Ok I have faced this issue in magento 1 too and I am not able to understand why addAttributeToSelect(*) never works.

When I call my product collection I want the collection will all the possible attributes.

Here is the product code

    protected $productCollection;

    public function __construct(
        ...
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        ...
    ) {
        $this->productCollection = $productCollectionFactory->create();
    }

    public function execute() {

        //this does not works
        $this->productCollection->addAttributeToSelect(['*']);

        $this->productCollection->addAttributeToSelect('*');

        $this->productCollection->addAttributeToSelect(['*'], 'inner');


        // If I call each attribute individually it will work
        $this->productCollection->addAttributeToSelect(['name', 'image'], 'inner');
    }

If I see the magento Magento\Catalog\Model\ResourceModel\Product\Collection class it has the code but still it does not work

public function addAttributeToSelect($attribute, $joinType = false)
    {
        if ($this->isEnabledFlat()) {
            if (!is_array($attribute)) {
                $attribute = [$attribute];
            }
            foreach ($attribute as $attributeCode) {
                if ($attributeCode == '*') {
                    foreach ($this->getEntity()->getAllTableColumns() as $column) {
                        $this->getSelect()->columns('e.' . $column);
                        $this->_selectAttributes[$column] = $column;
                        $this->_staticFields[$column] = $column;
                    }
                } else {
                    $columns = $this->getEntity()->getAttributeForSelect($attributeCode);
                    if ($columns) {
                        foreach ($columns as $alias => $column) {
                            $this->getSelect()->columns([$alias => 'e.' . $column]);
                            $this->_selectAttributes[$column] = $column;
                            $this->_staticFields[$column] = $column;
                        }
                    }
                }
            }
            return $this;
        }
        return parent::addAttributeToSelect($attribute, $joinType);
    }

why It does not works and what can be the way around that I need not load each and every item of collection again to get all data.

2

That worked for me all the time.

$this->productCollection->addAttributeToSelect(array('*'))->load();

also look at this addAttributeToSelect in magento2

| improve this answer | |
  • can you print and past your data as this never works for me – Priyank Nov 30 '16 at 13:46
  • @userpk use load() function at end then it will load attributes. – Qaisar Satti Nov 30 '16 at 13:54
  • I still get the same result – Priyank Dec 1 '16 at 13:09
  • @userpk it load for me all price attributes custom options and etc. i don't know why not working for you – Qaisar Satti Dec 1 '16 at 13:11
  • Do u use the same collection factory as I have mentioned in my question ? – Priyank Dec 1 '16 at 13:12
2

I believe each of the attributes you need to retrieve using addAttributeToSelect('*') need to be in the product flat table - if you have this enabled (therefore, all attributes must be set to visible on front end - at least I remember that this is how it worked in Magento 1)

| improve this answer | |
  • So when I am using $this->productCollection you mean it fetches data from flat tables? – Priyank Dec 23 '17 at 10:28
  • I believe so, the extra attributes are selected from there. – BAF Dec 30 '17 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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