Magento user created attribute for products is not saved
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,
开发者_如何学C '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.
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
Your attributes are not saved by default. You can use:
$product->getResource()->saveAttribute($product, "name_of_your_attribute");
精彩评论