Magento:添加自定义 EAV 属性,该属性在实体创建时自动填充其默认值
有没有办法添加自定义 EAV 属性,该属性在创建新实体时自动设置为其默认值?
我将 eav_attribute.is_required
设置为 1
并将 eav_attribute.default_value
设置为 0
(默认值),但事实并非如此创建新对象时自动设置属性。
顺便说一句,EAV 实体类型是shipment
。在销售数据存储在平面表中之前,我正在安装 1.3.2.4。
编辑
Jonathan Day 问“你如何添加属性?”
在 ModuleDir\sql\module_setup\mysql4-install-0.1.0.php 中,我有以下代码:
$eav = new Mage_Eav_Model_Entity_Setup('sales_setup');
$eav->addAttribute('shipment', 'fieldname', array('type' => 'int'));
我也有此代码Magento 的更高版本(在销售实体从 EAV 变为平面表之后):
$w = $this->_conn;
$table = $this->getTable('sales_flat_shipment');
$w->addColumn($table, 'fieldname', 'int');
$w->addKey($table, 'fieldname', 'fieldname', 'index');
Jonathan Day 询问“您是否已检查该属性是否已使用正确的字段添加到 eav_attribute 中?”
是的,已添加到 eav_attribute。并且该属性是可设置和可获取的。
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('sales_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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查看
Varien_Object::getData()
(在/lib/Varien/Object.php
中),您可以看到以下行:...以及任何缺失的实例数据返回
$default
。因此,您在属性中设置的默认值将被忽略。由于任何未定义的“get”均由
getData()
处理,这意味着null
将始终是默认值。似乎唯一的选择是覆盖 Sales_Model_Order_Shipment (或任何实体模型)并提供自定义 getter。一个简单的例子是:
If you look at
Varien_Object::getData()
(in/lib/Varien/Object.php
) you can see the following line:...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 anull
will always be the default. It seems the only option is to overrideSales_Model_Order_Shipment
(or whichever entity model it is) and provide custom getters.A simple example would be: