1

I have the following code:

$storeId = 1;
$attributesData = array("price" => $data['price'], 'updated_at' => date('Y-m-d H:i:s'));
Mage::getSingleton('catalog/product_action')
    ->updateAttributes(array($data['product_id']), $attributesData, $storeId);

When I run this code, the price is updated but the updated_at column in catalog_product_entity is not (it does this regardless of update_at being in $attributesData).

I know I could just hammer in a quick little update query to update the date myself (and I will if I have to), but that is not the "Magento" way of doing things. I also do not want to load up the entire model just to save it. That would defeat the entire purpose of using what I call the "fast way" of updating products.

Any ideas?

4

I think it's because updated_at is a static attribute not a 'variable' EAV attribute. You can't save it as you did. You have to save it with the help of the Product model.

Mage_Catalog_Model_Product_Action::updateAttributes() call the method of the resource model Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Action::updateAttributes() and in this method tries to get the attribute from the eav entity. But in the case of updated_at, it saves the attribute in a bad way. See below.

I tried your code and got an exception, I use Magento 1.5. You probably also have a silenced error. After debuggging, I saw the following sql query which is triggered when using your code:

INSERT INTO `mage_catalog_product_entity` (`entity_type_id`, `attribute_id`, `store_id`, `entity_id`, `value`) VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE `value`=VALUES(`value`)


0   (string:1) 4    
1   (string:3) 498  
2   (int) 0 
3   (int) 303   
4   (string:19) 2013-01-30 15:47:00 


SQLSTATE[42S22]: Column not found: 1054 Unknown column 'attribute_id' in 'field list'

The class Mage_Catalog_Model_Resource_Eav_Mysql4_Abstract and the method _saveAttributeValue show how the query is created:

$table   = $attribute->getBackend()->getTable();

    /**
     * If we work in single store mode all values should be saved just
     * for default store id
     * In this case we clear all not default values
     */
    if (Mage::app()->isSingleStoreMode()) {
        $storeId = $this->getDefaultStoreId();
        $write->delete($table, join(' AND ', array(
            $write->quoteInto('attribute_id=?', $attribute->getAttributeId()),
            $write->quoteInto('entity_id=?', $object->getEntityId()),
            $write->quoteInto('store_id<>?', $storeId)
        )));
    }

    $bind = array(
        'entity_type_id'    => $attribute->getEntityTypeId(),
        'attribute_id'      => $attribute->getAttributeId(),
        'store_id'          => $storeId,
        'entity_id'         => $object->getEntityId(),
        'value'             => $this->_prepareValueForSave($value, $attribute)
    );

For me it's definitely not the way to save this attribute. Use the model instead. You will be sure of the result.

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