Is there a way to add a custom EAV attribute that automatically gets set to its default value upon creation of a new entity?
I set eav_attribute.is_required
to 1
and eav_attribute.default_value
to 0
(the default value), but it's not setting the attribute automatically when I create a new object.
By the way, the EAV entity type is shipment
. I'm working on an installation of 1.3.2.4, before sales data was stored in flat tables.
EDIT
Jonathan Day asked "how are you adding the attribute?"
In ModuleDir\sql\module_setup\mysql4-install-0.1.0.php, I have the following code:
$eav = new Mage_Eav_Model_Entity_Setup('sa开发者_如何学编程les_setup');
$eav->addAttribute('shipment', 'fieldname', array('type' => 'int'));
I also have the this code for later versions of Magento (after the sales entities went from EAV to flat tables):
$w = $this->_conn;
$table = $this->getTable('sales_flat_shipment');
$w->addColumn($table, 'fieldname', 'int');
$w->addKey($table, 'fieldname', 'fieldname', 'index');
Jonathan Day asked "Have you checked that the attribute is added to eav_attribute with the correct fields?"
Yes, it has been added to eav_attribute. And the attribute is settable and gettable.
If you look at Varien_Object::getData()
(in /lib/Varien/Object.php
) you can see the following line:
$default = null;
...and any instance of missing data returns $default
. So the default value you set in your attribute is being ignored.
Since any undefined 'get' is handled by getData()
it means a null
will always be the default. It seems the only option is to override Sales_Model_Order_Shipment
(or whichever entity model it is) and provide custom getters.
A simple example would be:
function getSample()
{
if (!$this->hasSample())
return 0;
return $this->getData('sample');
}
精彩评论