Magento:无法使用安装脚本设置自定义订单属性的默认值
我在模块中使用 mysql4-install-1.0.0.php 为订单添加了自定义属性:
$installer = $this;
$installer->startSetup();
$installer->addAttribute('order', 'custom_status', array(
'type' => 'varchar',
'label' => 'My Status',
'note' => '',
'default' => "my_default_value",
'visible' => false,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'unique' => false
));
它有效 - 当我查看 sales_flat_order 表时,我看到新的 varchar表中的字段*custom_status*。但默认值是 NULL,而不是预期的 "my_default_value"
。有什么想法吗,为什么?
附言。安装脚本真的被执行了,我每次都将所有重置为初始状态。
UPD。 config.xml
<resources>
<mymodule_setup>
<setup>
<module>Company_Mymodule</module>
<class>Mage_Sales_Model_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</mymodule_setup>
***
I've added custom attribute to orders using mysql4-install-1.0.0.php in my module:
$installer = $this;
$installer->startSetup();
$installer->addAttribute('order', 'custom_status', array(
'type' => 'varchar',
'label' => 'My Status',
'note' => '',
'default' => "my_default_value",
'visible' => false,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'unique' => false
));
It works - when I look at sales_flat_order table, I see new varchar field *custom_status* in the table. But the default value is NULL instead of "my_default_value"
as expected there. Any ideas, why?
PS. Installation script is really executed, I reset all to initial state each time.
UPD. config.xml
<resources>
<mymodule_setup>
<setup>
<module>Company_Mymodule</module>
<class>Mage_Sales_Model_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</mymodule_setup>
***
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Magento Sales 模块具有旧 EAV 功能的伪模拟。它只需要数组中的类型属性即可在数据库中创建列。它还使用“网格”属性来确定是否需要在网格表示表中创建相同的列。
BTW,之前基于 EAV 的销售模块也没有使用 default、label、required 等属性。如果要为此属性设置默认值,则需要在保存事件之前创建订单观察者,并在字段为空时在其中设置此数据。
Magento Sales module has pseudo emulation of old EAV functionality. It takes only type property from your array to create a column in the database. Also it uses "grid" property for determining is it required to make the same column in grid representation table.
BTW, previous sales module that was based on EAV, was not using default, label, required and other properties as well. If you want to set a default value for this attribute you need create an observer for order before save event and set this data in it if field is empty.
确保您的 config.xml 中有这样的内容:
Mage_Sales_Model_Mysql4_Setup
非常重要!然后在安装脚本mysql4-install-0.1.0.php中:
Make sure you have it like this in your config.xml:
The
<class>Mage_Sales_Model_Mysql4_Setup</class>
is extremely important!Then in the install script mysql4-install-0.1.0.php: