Magento – addAttributeToSelect() does not work for newly created attributes

bugmagento-1.9product-attributeproduct-collection

I had to create a product collection that contains some newly created attributes. It was not possible to add these attributes to the collection ('inner' doesn't make a difference). Even reindexing didn't help.

Example code:

require_once('./app/Mage.php');
error_reporting(E_ALL);
ini_set('display_errors', 1);

umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$attributes = array(
    'name',
    'new_attribute'
);
$products = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect($attributes, 'inner');

Query for that collection:

SELECT `e`.*, `at_name`.`value` AS `name`, `at_new_attribute`.`value` AS `new_attribute`
FROM `catalog_product_entity` AS `e`
INNER JOIN `catalog_product_entity_varchar` AS `at_name`
    ON (`at_name`.`entity_id` = `e`.`entity_id`) AND (`at_name`.`attribute_id` = '71') AND (`at_name`.`store_id` = 0)
INNER JOIN `catalog_product_entity_varchar` AS `at_new_attribute`
    ON (`at_new_attribute`.`entity_id` = `e`.`entity_id`) AND (`at_new_attribute`.`attribute_id` = '514') AND (`at_new_attribute`.`store_id` = 0)

If i check for that attribute_id 514 in catalog_product_entity_varchar I get also an empty result. So neither null nor the "default value" is set.

SELECT *
FROM `catalog_product_entity_varchar`
WHERE attribute_id = 514

MySQL returned an empty result set

After using workaround from below I get 750 correct results (with value null).

Even when you set a "default value" for this newly created attribute, it is not populated to products when you add it to attribute set. The "default value" is only used if you create new products.

Steps to reproduce:

  1. add new attribute (use in product listing doesn't matter)
  2. assign attribute to attribute set
  3. reindex attributes/flat tables
  4. run code

Expected result:

Get a collection where $item->getNewAttribute() is null or empty string.

Current result:

Get an empty collection. ($products->getSize() is 0)


Workaround:

Fortunately i found an answer here: When setting a product attribute programmatically, can't retrieve it from a collection

  1. open product grid
  2. select all
  3. update attributes
  4. submit
  5. check "change" checkbox for "attributecode" attribute but leave input field empty
  6. save

For my understanding attribute values (null or "default value") should be added to DB when assing a attribute to a set, but it doesn't happen. Products only appear in product collection, if:

  • product is saved manually, or
  • workaround is used (for multible products)

Any ideas where to fix this in code?

Best Answer

You can't get theme in your collection because, Only the attributes that are marked be used in product listing to YES are copyed to the flat tables. Mark it like this in Catalog > Attributes > Manage Attributes > {select your attribute}, reindex everything and now you have your attributes.

EDIT:

It should work by doing this config! anyway if you don't set used in product listing to yes, You will never get the attributes in the collection, i explained you because for doing this config, Magento copyed them to the flat tables. and don't forget to reindex all !

Maybe you have some issue in your collection, try this one:

$products = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect(array('name', 'price', 'yourattribute'));
Related Topic