1

I have create custom admin FORM and use 2 product attributes as field select option but in getting error.

"You cannot define a correlation name 'tdv' more than once"

If i have use single attribute in Form then it's working fine. Any idea how can it resolve ?

Here is my code:

static public function getOptionArray3()
        {
            $data_array=array();

            $attributeCode = 'size';
            $entityType = 'catalog_product';

            $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
            $attributeInfo = $objectManager->create(\Magento\Eav\Model\Entity\Attribute::class)
                                           ->loadByCode($entityType, $attributeCode);



            $attributeId = $attributeInfo->getAttributeId();

            $attributeOptionAll = $objectManager->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class)
                                    ->setPositionOrder('asc')
                                    ->setAttributeFilter($attributeId)                                               
                                    ->setStoreFilter()
                                    ->load();  

            foreach ($attributeOptionAll as $attributeOption) {
                $data_array[$attributeOption->getOptionId()] = $attributeOption->getValue();
            }                                                    

            return($data_array);
        }

        static public function getOptionArray6()
        {
            $data_array=array();

            $attributeCode = 'use_the_mattress';
            $entityType = 'catalog_product';

            $objectManager1 =  \Magento\Framework\App\ObjectManager::getInstance();
            $attributeInfo = $objectManager1->create(\Magento\Eav\Model\Entity\Attribute::class)
                                           ->loadByCode($entityType, $attributeCode);

            $attributeId = $attributeInfo->getAttributeId();

            $attributeOptionAll = $objectManager1->get(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class)
                                    ->setPositionOrder('asc')
                                    ->setAttributeFilter($attributeId)                                               
                                    ->setStoreFilter()
                                    ->load();  

            foreach ($attributeOptionAll as $attributeOption) {
                $data_array[$attributeOption->getOptionId()] = $attributeOption->getValue();
            }                                                    

            return($data_array);
        }
  • can you post your code here – fmsthird Jan 3 '19 at 12:35
3

try below code. It should work. You've used get method in getting collection. That's why it was working for first object only.

$_objectManager->create() creates new instance of the object, no-matter-what

$_objectManager->get() first tries to find shared instance (already created), if it's not found - it just creates new shared instance

static public function getOptionArray3()
{
    $data_array=array();

    $attributeCode = 'size';
    $entityType = 'catalog_product';

    $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
    $attributeInfo = $objectManager->create(\Magento\Eav\Model\Entity\Attribute::class)
                                   ->loadByCode($entityType, $attributeCode);



    $attributeId = $attributeInfo->getAttributeId();

    $attributeOptionAll = $objectManager->create(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class)
                            ->setPositionOrder('asc')
                            ->setAttributeFilter($attributeId)                                               
                            ->setStoreFilter()
                            ->load();  

    foreach ($attributeOptionAll as $attributeOption) {
        $data_array[$attributeOption->getOptionId()] = $attributeOption->getValue();
    }                                                    

    return($data_array);
}

static public function getOptionArray6()
{
    $data_array=array();

    $attributeCode = 'use_the_mattress';
    $entityType = 'catalog_product';

    $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
    $attributeInfo = $objectManager->create(\Magento\Eav\Model\Entity\Attribute::class)
                                   ->loadByCode($entityType, $attributeCode);

    $attributeId = $attributeInfo->getAttributeId();

    $attributeOptionAll = $objectManager->create(\Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection::class)
                            ->setPositionOrder('asc')
                            ->setAttributeFilter($attributeId)                                               
                            ->setStoreFilter()
                            ->load();  

    foreach ($attributeOptionAll as $attributeOption) {
        $data_array[$attributeOption->getOptionId()] = $attributeOption->getValue();
    }                                                    

    return($data_array);
}

Note: I will not recommend you to use Object Manager, instead use proper method to call function and avoid duplication of code.

|improve this answer|||||
  • Thanks , Abhishek Panchal really you save me time :) – manoj pal Jan 3 '19 at 18:23
0

Rather to use Objectmanager try below code its worked for me:

public function __construct(
    ............,
    ............,
    \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory $optionFactory,
    \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\CollectionFactory $attributeOptionCollection
) {
    $this->optionFactory = $optionFactory;
    $this->_attributeOptionCollection = $attributeOptionCollection;
}

public function execute()
{
    $optionFactory = $this->optionFactory->create();
    $optionFactory->load('5529'); // load by option value
    $attributeId = $optionFactory->getAttributeId(); // atribute id of given option value
    $attributeOptionCollectionFac = $this->_attributeOptionCollection->create();
    $optionData = $attributeOptionCollectionFac
                        ->setPositionOrder('asc')
                        ->setAttributeFilter($attributeId)
                        ->setIdFilter('5529')
                        ->setStoreFilter()
                        ->load();
}
|improve this answer|||||

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.