2014-11-04 81 views
4

我需要在我的magento商店中創建4個新的客戶屬性。我創建瞭如下一個模塊可以這樣做:Magento - 創建新的客戶屬性

Customerattribute > 
    etc > config.xml 
    Model > Mysql4 > Setup.php 
    sql > customerattribute_setup > mysql4-install-0.0.1.php 

config.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<config> 
    <modules> 
    <Custom_Customerattribute> 
     <version>0.0.1</version> 
    </Custom_Customerattribute> 
    </modules> 
    <global> 
     <resources> 
      <customerattribute_setup> 
       <setup> 
        <module>Custom_Customerattribute</module> 
        <class>Custom_Customerattribute_Model_Mysql4_Setup</class> 
       </setup> 
       .... 
      </customerattribute_setup> 
     </resources> 
    </global> 
</config> 

Setup.php

class Custom_Customerattribute_Model_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup 
{ 
    /** 
    * This method returns true if the attribute exists. 
    * 
    * @param string|int $entityTypeId 
    * @param string|int $attributeId 
    * @return bool 
    */ 
    public function attributeExists($entityTypeId, $attributeId) 
    { 
     try 
     { 
      $entityTypeId = $this->getEntityTypeId($entityTypeId); 
      $attributeId = $this->getAttributeId($entityTypeId, $attributeId); 
      return !empty($attributeId); 
     } 
     catch(Exception $e) 
     { 
      return FALSE; 
     } 
    } 
} 

mysql4安裝-0.0.1.php

$installer = $this; 
$installer->startSetup(); 
$entity = $installer->getEntityTypeId('customer'); 

if(!$installer->attributeExists($entity, 'paypal')) { 
    $installer->removeAttribute($entity, 'paypal'); 
} 

$installer->addAttribute($entity, 'paypal', array(
     'type' => 'text', 
     'label' => 'Paypal', 
     'input' => 'text', 
     'visible' => TRUE, 
     'required' => FALSE, 
     'default_value' => '', 
     'adminhtml_only' => '0' 
)); 

$forms = array(
    'adminhtml_customer', 
    'customer_account_edit' 
); 
$attribute = Mage::getSingleton('eav/config')->getAttribute($installer->getEntityTypeId('customer'), 'paypal'); 
$attribute->setData('used_in_forms', $forms); 
$attribute->save(); 

$installer->endSetup(); 

此工程的0123我的第一個客戶屬性但我現在想要能夠添加3個其他人。我希望,如果我改變mysql4-install-0.0.1.php文件這樣說:

$installer = $this; 
$installer->startSetup(); 
$entity = $installer->getEntityTypeId('customer'); 

if(!$installer->attributeExists($entity, 'attribute_2')) { 
    $installer->removeAttribute($entity, 'attribute_2'); 
} 

$installer->addAttribute($entity, 'attribute_2', array(
     'type' => 'text', 
     'label' => 'Attribute 2', 
     'input' => 'text', 
     'visible' => TRUE, 
     'required' => FALSE, 
     'default_value' => '', 
     'adminhtml_only' => '0' 
)); 

$forms = array(
    'adminhtml_customer', 
    'customer_account_edit' 
); 
$attribute = Mage::getSingleton('eav/config')->getAttribute($installer->getEntityTypeId('customer'), 'attribute_2'); 
$attribute->setData('used_in_forms', $forms); 
$attribute->save(); 

$installer->endSetup(); 

,並上傳新文件,又回到到網站attribute_2將被添加,但事實並非如此。

爲什麼一次,但不會再次這項工作?

回答

10

您需要在此添加新的文件(安裝腳本)和文件名應該是,

mysql4-upgrade-0.0.2-0.0.1.php 

所以,現在你可以添加你的安裝腳本一樣,

$installer = $this; 
$installer->startSetup(); 
$entity = $installer->getEntityTypeId('customer'); 

if(!$installer->attributeExists($entity, 'attribute_2')) { 
    $installer->removeAttribute($entity, 'attribute_2'); 
} 

$installer->addAttribute($entity, 'attribute_2', array(
     'type' => 'text', 
     'label' => 'Attribute 2', 
     'input' => 'text', 
     'visible' => TRUE, 
     'required' => FALSE, 
     'default_value' => '', 
     'adminhtml_only' => '0' 
)); 

$forms = array(
    'adminhtml_customer', 
    'customer_account_edit' 
); 
$attribute = Mage::getSingleton('eav/config')->getAttribute($installer->getEntityTypeId('customer'), 'attribute_2'); 
$attribute->setData('used_in_forms', $forms); 
$attribute->save(); 

$installer->endSetup(); 

而你需要也更新config.xml文件。因此,它應該是,

<?xml version="1.0" encoding="UTF-8"?> 
<config> 
    <modules> 
    <Custom_Customerattribute> 
     <version>0.0.2</version> 
    </Custom_Customerattribute> 
    </modules> 
    <global> 
     <resources> 
      <customerattribute_setup> 
       <setup> 
        <module>Custom_Customerattribute</module> 
        <class>Custom_Customerattribute_Model_Mysql4_Setup</class> 
       </setup> 
       .... 
      </customerattribute_setup> 
     </resources> 
    </global> 
</config> 

欲瞭解更多信息,請here。如果您有任何疑問,請在這裏留言。

更新: 對不起。文件名應該是這樣的,

mysql4-upgrade-0.0.2-0.0.1.php 

core_resource表包含模塊條目。現在0.0.2也被更新。所以magento不會查找你的模塊(更新)的XML文件加載。所以你需要的文件名又變成mysql4-upgrade-0.0.3-0.0.2.php或刪除數據庫中的該條目和重命名你的模塊版本,以新鮮的像mysql4-upgrade-0.0.0.php

更新2:

在這裏,我賦予了新的代碼,我檢查我在當地的做工精細,

應用程序/代碼/本地/包/ MODULENAME的/ etc/config.xml中

<?xml version="1.0"?> 
<config> 
    <modules> 
    <Packagename_Modulename> 
     <version>0.0.0</version> 
    </Packagename_Modulename> 
    </modules> 
    <global> 
    <helpers> 
     <modulename> 
     <class>Packagename_Modulename_Helper</class> 
     </modulename> 
    </helpers> 
    <models> 
     <modulename> 
     <class>Packagename_Modulename_Model</class> 
     <resourceModel>modulename_mysql4</resourceModel> 
     </modulename> 
    </models> 
    <resources> 
     <customerattribute1415104755_setup> 
     <setup> 
      <module>Packagename_Modulename</module> 
      <class>Mage_Customer_Model_Entity_Setup</class> 
     </setup> 
     <connection> 
      <use>core_setup</use> 
     </connection> 
     </customerattribute1415104755_setup> 
     <customerattribute1415104755_write> 
     <connection> 
      <use>core_write</use> 
     </connection> 
     </customerattribute1415104755_write> 
     <customerattribute1415104755_read> 
     <connection> 
      <use>core_read</use> 
     </connection> 
     </customerattribute1415104755_read> 
    </resources> 
    </global> 
</config> 

應用程序/代碼/本地/包/模塊名/助手/Data.php

<?php 
class Packagename_Modulename_Helper_Data extends Mage_Core_Helper_Abstract 
{ 
} 

應用程序/代碼/本地/包/ MODULENAME/SQL/customerattribute1415104755_setup/mysql4安裝-0.0.0。PHP

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


$installer->addAttribute("customer", "myattrbute1", array(
    "type"  => "varchar", 
    "backend" => "", 
    "label" => "My attribute-1", 
    "input" => "text", 
    "source" => "", 
    "visible" => true, 
    "required" => false, 
    "default" => "", 
    "frontend" => "", 
    "unique"  => false, 
    "note"  => "" 

    )); 

     $attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "myattrbute1"); 


$used_in_forms=array(); 

$used_in_forms[]="adminhtml_customer"; 
$used_in_forms[]="checkout_register"; 
$used_in_forms[]="customer_account_create"; 
$used_in_forms[]="customer_account_edit"; 
$used_in_forms[]="adminhtml_checkout"; 
     $attribute->setData("used_in_forms", $used_in_forms) 
     ->setData("is_used_for_customer_segment", true) 
     ->setData("is_system", 0) 
     ->setData("is_user_defined", 1) 
     ->setData("is_visible", 1) 
     ->setData("sort_order", 100) 
     ; 
     $attribute->save(); 




$installer->addAttribute("customer", "myattrbute2", array(
    "type"  => "varchar", 
    "backend" => "", 
    "label" => "My attribute-2", 
    "input" => "text", 
    "source" => "", 
    "visible" => true, 
    "required" => false, 
    "default" => "", 
    "frontend" => "", 
    "unique"  => false, 
    "note"  => "" 

    )); 

     $attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "myattrbute2"); 


$used_in_forms=array(); 

$used_in_forms[]="adminhtml_customer"; 
$used_in_forms[]="checkout_register"; 
$used_in_forms[]="customer_account_create"; 
$used_in_forms[]="customer_account_edit"; 
$used_in_forms[]="adminhtml_checkout"; 
     $attribute->setData("used_in_forms", $used_in_forms) 
     ->setData("is_used_for_customer_segment", true) 
     ->setData("is_system", 0) 
     ->setData("is_user_defined", 1) 
     ->setData("is_visible", 1) 
     ->setData("sort_order", 100) 
     ; 
     $attribute->save(); 




$installer->addAttribute("customer", "myattrbute3", array(
    "type"  => "varchar", 
    "backend" => "", 
    "label" => "My attribute-3", 
    "input" => "text", 
    "source" => "", 
    "visible" => true, 
    "required" => false, 
    "default" => "", 
    "frontend" => "", 
    "unique"  => false, 
    "note"  => "" 

    )); 

     $attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "myattrbute3"); 


$used_in_forms=array(); 

$used_in_forms[]="adminhtml_customer"; 
$used_in_forms[]="checkout_register"; 
$used_in_forms[]="customer_account_create"; 
$used_in_forms[]="customer_account_edit"; 
$used_in_forms[]="adminhtml_checkout"; 
     $attribute->setData("used_in_forms", $used_in_forms) 
     ->setData("is_used_for_customer_segment", true) 
     ->setData("is_system", 0) 
     ->setData("is_user_defined", 1) 
     ->setData("is_visible", 1) 
     ->setData("sort_order", 100) 
     ; 
     $attribute->save(); 




$installer->addAttribute("customer", "myattrbute4", array(
    "type"  => "varchar", 
    "backend" => "", 
    "label" => "My attribute-4", 
    "input" => "text", 
    "source" => "", 
    "visible" => true, 
    "required" => false, 
    "default" => "", 
    "frontend" => "", 
    "unique"  => false, 
    "note"  => "" 

    )); 

     $attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "myattrbute4"); 


$used_in_forms=array(); 

$used_in_forms[]="adminhtml_customer"; 
$used_in_forms[]="checkout_register"; 
$used_in_forms[]="customer_account_create"; 
$used_in_forms[]="customer_account_edit"; 
$used_in_forms[]="adminhtml_checkout"; 
     $attribute->setData("used_in_forms", $used_in_forms) 
     ->setData("is_used_for_customer_segment", true) 
     ->setData("is_system", 0) 
     ->setData("is_user_defined", 1) 
     ->setData("is_visible", 1) 
     ->setData("sort_order", 100) 
     ; 
     $attribute->save(); 



$installer->endSetup(); 

最後啓用, 應用程序的/ etc /模塊的模塊/ Packagename_Modulename.xml

<?xml version="1.0"?> 
<config> 
    <modules> 
    <Packagename_Modulename> 
     <active>true</active> 
     <codePool>local</codePool> 
     <version>0.0.0</version> 
    </Packagename_Modulename> 
    </modules> 
</config> 
+0

謝謝,但這仍然無法正常工作。我已經刷新所有緩存之前和之後,清除緩存和緩存存儲。正如你所說的那樣也稱爲安裝文件 - 'mysql4-install-0.0.1-0.0.2.php'和'mysql4-install-0.0.2.php'沒有運氣 – 2014-11-04 10:38:11

+0

對不起。文件名應該像這樣mysql4-install-0.0.2-0.0.1.php。我已經更新了我的答案,請檢查它。 – Elavarasan 2014-11-04 10:46:50

+0

更新了文件名,但仍然無法使用。我查看了'core_resource'數據庫表,並可以看到該模塊的更新的0.0.2版本。在網上看看它看起來像刪除,然後再次運行會解決,它確實,不知道爲什麼?! – 2014-11-04 11:08:54

0

我覺得孤單早些時候在代碼中的錯誤提出:

if(!$installer->attributeExists($entity, 'attribute_2')) { 
    $installer->removeAttribute($entity, 'attribute_2'); 
} 

...似乎在說'如果新屬性不存在 - 刪除它'

你可能想要這個:

if ($installer->attributeExists($entity, 'attribute_2')) { 
    $installer->removeAttribute($entity, 'attribute_2'); 
} 
相關問題