2012-07-05 54 views
1

我在Magento中創建了我的模塊,但它需要爲所有產品分配附加屬性。我做了安裝腳本,但它不工作:Magento:使用模塊安裝腳本添加產品屬性

<?php 

$installer = $this; 
$installer->startSetup(); 

$installer->run(" 
    INSERT IGNORE INTO `{$installer->getTable('eav/attribute')}` (`attribute_id`, `entity_type_id`, `attribute_code`, `attribute_model`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_required`, `is_user_defined`, `default_value`, `is_unique`, `note`) VALUES 
    SELECT 1 + coalesce((SELECT max(attribute_id) FROM `{$installer->getTable('eav/attribute')}`),0),4,`is_accepted`,NULL,NULL,`int`,NULL,NULL,`boolean`,`Accepted`,NULL,`eav/entity_attribute_source_boolean`,1,1,'0',0,NULL); 
"); 

$installer->endSetup(); 

Config.xml文件:

<adminhtml> 
<acl> 
    <module_setup> 
        <setup> 
         <module>My_module</module> 
        </setup> 
        <connection> 
         <use>core_setup</use> 
        </connection> 
       </module_setup> 

       <module_write> 
        <connection> 
         <use>core_write</use> 
        </connection> 
       </module_write> 

       <module_read> 
        <connection> 
         <use>core_read</use> 
        </connection> 
       </module_read> 
      </resources> 
     </acl> 
</adminhtml> 

這裏有什麼問題?

回答

6

首先,這不是一個有效的config.xml。設置類配置如下:

<config> 
    ... 
    <global> 
     ... 
     <resources> 
      ... 
      <your_module_setup> 
       <setup> 
        <module>Your_Module</module> 
        <class>Mage_Eav_Model_Entity_Setup</class> 
       </setup> 
      </your_module_setup> 
      ... 
     </resources> 
     ... 
    </global> 
    ... 
</config> 

,而不是Mage_Eav_Model_Entity_Setup你也可以用你自己的設置類,但它應該繼承Mage_Eav_Model_Entity_Setup,所以你可以用它代替手工鍛造的SQL查詢的addAttribute。

那麼你的安裝腳本應類似於此:

$installer = $this; 
$installer->startSetup(); 
/* 
* adds product unit attribute to product 
*/ 
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'productunit_id', array(
    'label' => Mage::helper('productunits')->__('Quantity Unit'), 
    'type' => 'int', 
    'input' => 'select', 
    'source' => SGH_ProductUnits_Model_Entity_Attribute_Source_Units::MODEL, 
    'backend' => SGH_ProductUnits_Model_Entity_Attribute_Backend_Units::MODEL, 
    'required' => 1, 
    'global' => 1, 
    'note' => Mage::helper('productunits')->__('This will be displayed next to any Qty value.') 
)); 
$installer->endSetup(); 

這是我的代碼,增加了一個數量單位的屬性,不要使用類常量的混淆,這些都只是相應的類別名。

+0

好主意,但仍然得到一個錯誤,沒有方法addAttribute()在安裝程序中:'致命的錯誤:調用未定義的方法Mage_Core_Model_Resource_Setup :: addAttribute()'編輯:對不起,我忘了你寫了_代替Mage_Eav_Model_Entity_Setup你也可以使用你的自己的安裝類,但它應該繼承Mage_Eav_Model_Entity_Setup,所以你可以使用addAttribute,而不是手工僞造SQL查詢._ –

+0

好吧,我在我的config.xml文件中添加了' Mage_Eav_Model_Entity_Setup',它工作。謝謝! :) –

3

您的<module_setup>節點必須在config/global/resources之下,而不是在config/adminhtml/acl之下。

+0

好吧,你是對的。但現在我有錯誤:'SQLSTATE [42000]:語法錯誤或訪問衝突:1064您的SQL語法錯誤;檢查對應於您的MySQL服務器版本的手冊,以便在第2行'SELECT max(attribute_id)+1 FROM'eav_attribute'),4,'is_accepted',NULL,NULL,'int'附近使用正確的語法。 –

+1

如果您的原始問題已被解答,請發佈一個單獨的問題。 StackOverflow並不像一個擁有永無止境的線程的論壇,每個問題都有獨立的帖子。 –

相關問題