2011-05-11 46 views
37

我當前的目標是增加其應顯示預定義選項(從後臺中編輯條目的模型,完成加載)作爲選擇新的客戶屬性(int型)。 我正確使用方法$installer->addAttribute()掙扎,尤其是指定正確的源選項。另一個問題是新的屬性不會保存到eav_entity_attribute表添加屬性客戶實體

我在Magento的CE 1.5.1.0

回答

71

這是text呈示基本int屬性代碼:

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

$setup = new Mage_Eav_Model_Entity_Setup('core_setup'); 

$entityTypeId  = $setup->getEntityTypeId('customer'); 
$attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId); 
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId); 

$setup->addAttribute('customer', 'your_attribute_code_here', array(
    'input'   => 'text', 
    'type'   => 'int', 
    'label'   => 'Some textual description', 
    'visible'  => 1, 
    'required'  => 0, 
    'user_defined' => 1, 
)); 

$setup->addAttributeToGroup(
$entityTypeId, 
$attributeSetId, 
$attributeGroupId, 
'your_attribute_code_here', 
'999' //sort_order 
); 

$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'your_attribute_code_here'); 
$oAttribute->setData('used_in_forms', array('adminhtml_customer')); 
$oAttribute->save(); 

$setup->endSetup(); 

添加屬性不尋常的步驟是setData('used_in_forms')這似乎是唯一的客戶屬性。沒有它,該字段將不會被渲染,當然不會在adminhtml中。你可以看到有效選項爲這個陣列中的customer_form_attribute數據庫表。

在使用預定義的選項select而言,這是你所需要的:

$iAttributeId = $installer->getAttributeId($entityTypeId, 'your_attribute_code_here'); 
$aClasses = array('TV','DVD','Home Theatre','Air Conditioner','Stereo/Hifi','Game Console','Camcorder','VCR','Set Top Box','PVR'); 
$aOption = array(); 
$aOption['attribute_id'] = $iAttributeId; 

for($iCount=0;$iCount<sizeof($aClasses);$iCount++){ 
    $aOption['value']['option'.$iCount][0] = $aClasses[$iCount]; 
} 
$setup->addAttributeOption($aOption); 

這裏是一個walk-through上使用自定義源爲您的下拉

希望這有助於
JD

+0

感謝您的答覆,我將給予這是一個嘗試。至於預定義的選項,他們不應該在我的情況下是靜態的,但可以從管理界面編輯,這就是爲什麼我試圖使用'源'選項 – Zifius 2011-05-11 10:37:17

+0

編輯鏈接動態選項。如果您使用的是提供客戶屬性管理器的Magento版本,則我可以在管理員中編輯預定義選項的示例,它只是一個起始集。 – 2011-05-11 13:18:15

+0

剛試過你的代碼,它的工作就像一個魅力,addAttributeToGroup調用真的很有幫助。另外我設置了源選項。 – Zifius 2011-05-17 09:48:49

23

@Jonathan Day的回答非常好,對我的幫助非常大。但是 - 只要你設置你的setupMage_Customer_Model_Entity_Setup,然後Magento的可以做所有的工作對您:

<!-- config.xml Example --> 
<?xml version="1.0"?> 
<config> 
    <global> 
     <resources> 
      <acme_module_setup> 
       <setup> 
        <module>Acme_Module</module> 
        <class>Mage_Customer_Model_Entity_Setup</class> 
       </setup> 
       <connection> 
        <use>core_setup</use> 
       </connection> 
      </acme_module_setup> 
     </resources> 
    </global> 
</config> 

這裏是mysql4-install-X.X.X.php文件:

<?php 

$installer = $this; 
/* @var $installer Mage_Customer_Model_Entity_Setup */ 

$installer->startSetup(); 

$installer->addAttribute(
    'customer', 
    'acme_imported', 
    array(
     'group'    => 'Default', 
     'type'     => 'int', 
     'label'    => 'Imported into Acme', 
     'input'    => 'select', 
     'source'    => 'eav/entity_attribute_source_boolean', 
     'global'    => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, 
     'required'    => 0, 
     'default'    => 0, 
     'visible_on_front'  => 1, 
     'used_for_price_rules' => 0, 
     'adminhtml_only'  => 1, 
    ) 
); 

$installer->endSetup(); 

以上adminhtml_only將爲您處理所有的used_in_forms邏輯。此外,定義group將其分配給屬性組的照顧。

+3

使用1.7.0.2 CE,* adminhtml_only *技巧對我無效。我仍然需要添加:'Mage :: getSingleton('eav/config') - > getAttribute('customer',$ custom_attribute_id) - > setData('used_in_forms',array('adminhtml_customer')) - > save()'該屬性顯示在後端。 – 2013-03-30 13:42:07

4

只是你必須通過下面的腳本下您的自定義模塊的MySQL安裝文件添加客戶屬性。

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


$installer->addAttribute("customer", "yourattributename", array(
    "type"  => "int", 
    "backend" => "", 
    "label" => "Bad Customer", 
    "input" => "select", 
    "source" => "eav/entity_attribute_source_boolean", 
    "visible" => true, 
    "required" => false, 
    "default" => "", 
    "frontend" => "", 
    "unique"  => false, 
    "note"  => "" 

    )); 

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

下面的腳本用於在那裏想Alex和韭菜都爲我工作使用客戶屬性

$used_in_forms=array(); 

$used_in_forms[]="adminhtml_customer"; 
     $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", 0) 
     ->setData("sort_order", 100) 
     ; 
     $attribute->save(); 

$installer->endSetup(); 
0

該解決方案提供。 只有我必須添加setter函數在我們AccountController.php

$customer->setProfession($this->getRequest()->getPost('profession')) 
         ->save(); // Added for updating Profession 

其中,「專業」是我的自定義屬性。

3

這個博客帖子有Magento的EAV模型與領域的參數和含義詳細描述了非常詳盡的解釋: http://www.solvingmagento.com/magento-eav-system/

+3

歡迎來到SO!帶有諸如此類鏈接的答案應該包含鏈接的重要部分,因爲鏈接可能會斷開並且不再可訪問。 – hichris123 2013-12-05 01:05:12

相關問題