开发者

Magento: can't set the default value of custom order attribute using installation script

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 开发者_StackOverflowreset 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>
 ***


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.


Make sure you have it like this in your config.xml:

        <resources>
            <customattr_setup>
                <setup>
                    <module>Yourcompany_ModuleName</module>
                    <class>Mage_Sales_Model_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </customattr_setup>
        </resources>

The <class>Mage_Sales_Model_Mysql4_Setup</class> is extremely important!

Then in the install script mysql4-install-0.1.0.php:

<?php
$installer = $this;
$installer->startSetup();

$installer->addAttribute(
    'order', 
    'customattr', 
    array(
        'type' => 'float', // or whatever you want here...
        'grid' => false
    )
);
$installer->endSetup();
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜