views:

409

answers:

2

I am fighting an apparently simple thing for about two days now. I hope somebody can help.

I created the myUpdate EAV attribute

class Company_Module_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
{
    public function getDefaultEntities()
    {
        return array(
            'catalog_product' => array(
                'entity_model'      => 'catalog/product',
                'attribute_model'   => 'catalog/resource_eav_attribute',
                'table'             => 'catalog/product',
                'additional_attribute_table' => 'catalog/eav_attribute',
                'entity_attribute_collection' => 'catalog/product_attribute_collection',
                'attributes'        => array(
                    'my_update' => array(
                        'label'             => 'My timestamp',
                        'type'              => 'datetime',
                        'input'             => 'date',
                        'default'           => '',
                        'class'             => 'validate-date',
                        'backend'           => 'eav/entity_attribute_backend_datetime',
                        'frontend'          => '',
                        'table'             => '',
                        'source'            => '',
                        'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                        'visible'           => true,
                        'required'          => false,
                        'user_defined'      => true,
                        'searchable'        => false,
                        'filterable'        => false,
                        'comparable'        => false,
                        'visible_on_front'  => false,
                        'visible_in_advanced_search' => false,
                        'unique'            => false,
                        'apply_to'          => 'simple',
                    )
                )
            )
        );
    }
}

The attribute is created OK and on install appears correctly in the list of attributes.

Later I do

// for product 1
$product->setMyUpdate($stringDate); // string this format: yyyy-MM-dd HH:mm:ss
$product->save(); // and saves without issues * in admin module

But later when I do:

$product = Mage::getModel('catalog/product')->load(1);
var_dump($product->getMyUpdate()); // returns null

Somehow the data is not really saved.. or I am not retrieving it correctly. Please advice on how to get the data and where the data is saved in the DB so I can check at if the insert is done correctly.

Thanks.

+1  A: 

Your attributes are not saved by default. You can use: $product->getResource()->saveAttribute($product, "name_of_your_attribute");

Wienczny
and the saved value? how do I specify the value?
Elzo Valugi
you will have to specify your value by using$product->setYourValue('value');before the 'saveAttribute'-Line
Wienczny
Thanks for the response, I appreciate it.
Elzo Valugi
+1  A: 

In the database, the attribute should be saved in the table catalog_product_entity_datetime organized by its attribute_id. First get the entity_type_id (since mine is apparently different than yours):

select entity_type_id from eav_entity_type where entity_type_code = 'catalog_product';

I get 4 on that last one, but substitute as applicable. The following query should list any saved values:

select * from catalog_product_entity_datetime where attribute_id = (select attribute_id from eav_attribute where entity_type_id = 4 and attribute_code = 'my_update');

To make sure that you have actually added the attribute to the database:

select attribute_id from eav_attribute where entity_type_id = 4 and attribute_code = 'my_update'

If you see values in that space, your problem may be that you haven't selected the attribute to be loaded. For instance, if you are loading products from a catalog product collection, there is a method necessary to load attributes:

$collection->addAttributeToSelect("my_update");

Please give more context on your product loading if you see that the data is actually saved.

Hope that helps.

Thanks! Joe

Joseph Mastey
I have the eav attribute but with 10 (catalog/product) instead of 4 (sales/order) and no entries in the catalog_product_entity_datetime.
Elzo Valugi
it worked. Turns out that the Magento guys think different about Objects than I do. I thought is normal to get even the custom fields when creating a product...
Elzo Valugi