Magento:添加自定义 EAV 属性,该属性在实体创建时自动填充其默认值

发布于 2024-10-05 02:39:56 字数 950 浏览 5 评论 0原文

有没有办法添加自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

裂开嘴轻声笑有多痛 2024-10-12 02:39:56

如果您查看 Varien_Object::getData() (在 /lib/Varien/Object.php 中),您可以看到以下行:

$default = null;

...以及任何缺失的实例数据返回$default。因此,您在属性中设置的默认值将被忽略。

由于任何未定义的“get”均由 getData() 处理,这意味着 null 将始终是默认值。似乎唯一的选择是覆盖 Sales_Model_Order_Shipment (或任何实体模型)并提供自定义 getter。

一个简单的例子是:

function getSample()
{
    if (!$this->hasSample())
        return 0;

    return $this->getData('sample');
}

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');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文