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?

  • It should work, ensure that \Magento\Catalog\Model\ResourceModel\Category\Collection` inject at your class . – Amit Bera Aug 3 '18 at 17:34
  • public function __construct( .... \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $catalogCategoryCollectionFactory, .... ) { $this->catalogCategoryCollectionFactory= $catalogCategoryCollectionFactory; } public function getMyCategoryCollection() { $categoryCollection =$this->catalogCategoryCollectionFactory->create(); $categoryCollection->addAttributeToSelect('image'); } – Amit Bera Aug 3 '18 at 17:39
  • Yes, it is injected – Dmitri Portenko Aug 3 '18 at 17:41
  • try with name file? – Amit Bera Aug 3 '18 at 17:41
  • Which type of category image you want to get. It's parent category or child category? – Rohan Hapani Aug 3 '18 at 18:10
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);
}

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.