4

I have entity

\Magento\Catalog\Model\ResourceModel\Category\Collection $categoryCollection

And I'm trying following

$categoryCollection->addAttributeToSelect('image');

but this doesn't work for me. Any ideas?

Dima Portenko
  • 1,200
  • 1
  • 7
  • 21

1 Answers1

0

I've done a small debug and found out that addAttributeToSelect have the second parameter

public function addAttributeToSelect($attribute, $joinType = false)

In my case catalog/frontend/flat_catalog_category configuration option was disabled. So I think for disabled flat category joinType should be true. I've added the following

\Magento\Catalog\Model\Indexer\Category\Flat\State $flatState

to the constructor. And this works for me like a charm

if ($this->flatState->isAvailable()) {
    $this->categoryCollection->addAttributeToSelect('image');
} else {
    $this->categoryCollection->addAttributeToSelect('image', true);
}
Dima Portenko
  • 1,200
  • 1
  • 7
  • 21