1

I am using below code to load product collection in custom module helper

$productCollection = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToSelect('custom_attribute_code')
                    ->addAttributeToFilter('status',1);

But addAttributeToSelect does not work, product collection does not include my custom_attribute_code attribute.

I also tried passing attributes in array().

It work if I pass attribute code twice as in below code

->addAttributeToSelect('custom_attribute_code','custom_attribute_code')

Anyone know why this strange issue is happening?

  • try with addAttributeToSelect('*') – Vivek Kumar Nov 25 '17 at 11:44
  • You want to attribute value in getData() ? if you use foreach and try get attribute value using product object them you can get it $product->getCustomAttribute(); – Vaibhav Ahalpara Nov 25 '17 at 11:46
  • I don't want load all attributes. anyways it does not make any difference – Piyush Nov 25 '17 at 11:46
  • I think addAttributeToSelect('*') will not display any attribute value in getData function attribute values can get by product object because it's EAV – Vaibhav Ahalpara Nov 25 '17 at 11:46
  • @VaibhavAhalpara I know how to get attributes from product object, the issue here is I don't want loop product collection instead I want array of values of a particular attribute – Piyush Nov 25 '17 at 11:49
1

When you use addAttributeToSelect function it will call

Mage_Eav_Model_Entity_Collection_Abstract:addAttributeToSelect($attribute, $joinType = false)

here your second parameter will used as joinType

In addAttributeToSelect method there is a code to check if $joinType has value and attribute value is static

LINE 430 : if ($joinType !== false && !$this->getEntity()->getAttribute($attribute)->isStatic()) {
        $this->_addAttributeJoin($attribute, $joinType);
    } 

Then it will call $this->_addAttributeJoin($attribute, $joinType);

Mage_Eav_Model_Entity_Collection_Abstract::_addAttributeJoin($attributeCode, $joinType = 'inner')

In this function $joinType is used as table join method

Line 1312 : $joinMethod = ($joinType == 'left') ? 'joinLeft' : 'join';

you can see here if $joinType == 'left' then it will select joinLeft else select join method

after that magento will join attribut table using above method and fetch data.

so basically you do not need to pass variable 2 times if you leave it blank then also it will works

    ->addAttributeToSelect('custom_attribute_code','custom_attribute_code')

the second attribute here is used to select table join method which is either left (joinLeft) or simple join

  • Yes , it should work if I leave second parameter blank, but It is not working in my case it is not working. It work if I pass second parameter (even any random value) – Piyush Nov 25 '17 at 12:43
  • blank means ->addAttributeToSelect('custom_attribute',''); if you not use second parameter then it will use false – Vaibhav Ahalpara Nov 25 '17 at 12:45
  • Can you tell me which magento version you have checked it – Piyush Nov 25 '17 at 12:46
  • version 1.9.3.4 – Vaibhav Ahalpara Nov 25 '17 at 12:46
  • still leaving second parameter blank does not giving me expected output, it works when I pass null (even passing false does not work) as second parameter – Piyush Nov 25 '17 at 12:49
0

do some temp filter using some non existing value, you will get that attribute into a result.

$productCollection = Mage::getModel('catalog/product')->getCollection()
              ->addAttributeToFilter('custom_attribute_code',array('neq'=>'111');
              ->addAttributeToFilter('status',1);
  • it may work in static queries but I can not add random value filter for dynamic queries – Piyush Nov 25 '17 at 12:09
  • that add not allow null filter – Sunil Patel Nov 25 '17 at 12:11

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.