3

I'm trying to get all attributes that belong to each product set back, either using the \Magento\Catalog\Model\Product\Attribute\ManagementFactory or \Magento\Catalog\Api\ProductAttributeManagementInterface.

It's works fine when there is one attribute set and returns the results expected; however if I want to get multiple attribute sets - the subsequent request is not as expected.

Digging into the code I've traced it down into \Magento\Eav\Model\AttributeManagement::getAttributes and echo'ing the select that the method generates shows the second query appends the attribute set filter as opposed to resetting it.

To recreate it you can use the following code; attribute set 4 is the 'default' one and 9 is the one that I have manually added.

Once the code below is ran it generates the following selects

$obj->create()->getAttributes(4);

SELECT `main_table`.*, `entity_attribute`.* FROM `eav_attribute` AS `main_table` INNER JOIN `eav_entity_attribute` AS `entity_attribute` ON entity_attribute.attribute_id = main_table.attribute_id WHERE (`entity_attribute`.`attribute_set_id` = '4') ORDER BY entity_attribute.sort_order ASC


$obj->create()->getAttributes(9); (note the 2 where clauses)

SELECT `main_table`.*, `entity_attribute`.* FROM `eav_attribute` AS `main_table` INNER JOIN `eav_entity_attribute` AS `entity_attribute` ON entity_attribute.attribute_id = main_table.attribute_id WHERE (`entity_attribute`.`attribute_set_id` = '4') AND (`entity_attribute`.`attribute_set_id` = '9') ORDER BY entity_attribute.sort_order ASC

<?php
require __DIR__ . '/app/bootstrap.php';

class TestApp extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface
{
    public function launch()
    {
        $this->_state->setAreaCode('adminhtml');


        $obj = $this->_objectManager->create('\Magento\Catalog\Model\Product\Attribute\ManagementFactory');
        $attr1 = $obj->create()->getAttributes(4);
        $attr2 = $obj->create()->getAttributes(9);        

        die();
    }

    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }
}

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('TestApp');
$bootstrap->run($app);

1 Answer 1

1
    /** @var  $coll \Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection */
    $coll = $this->_objectManager->create(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection::class);
    // add filter by entity type to get product attributes only
    // '4' is the default type ID for 'catalog_product' entity - see 'eav_entity_type' table)
    // or skip the next line to get all attributes for all types of entities
    $coll->addFieldToFilter(\Magento\Eav\Model\Entity\Attribute\Set::KEY_ENTITY_TYPE_ID, 4);
    $attrAll = $coll->load()->getItems();
1
  • Please read the comments in the code or ask your question directly.
    – Alex Gusev
    Jul 5, 2016 at 12:43

Your Answer

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

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