2017-05-11 81 views
7

我想在magento2的客戶表中添加自定義列(電話),並希望在客戶註冊時在此字段中添加值。如何在Magento2的客戶表中添加自定義列?

對於這第一個我在customer_entity table.While創建DB列(電話)在的Magento /客戶/控制器/帳號/ CreatePost創造客戶,當我調用$客戶 - > setTelephone( '1234567890')。 php中執行函數。它給出了一個錯誤在Magento/Customer/Model/Data/Customer.php中未定義函數setTelephone。但是我已經在這個模型中創建了這個函數。

的Magento /客戶/控制器/帳戶/ CreatePost.php

public function execute() 
{ 
    /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ 
    $resultRedirect = $this->resultRedirectFactory->create(); 
    if ($this->session->isLoggedIn() || !$this->registration->isAllowed()) { 
     $resultRedirect->setPath('*/*/'); 
     return $resultRedirect; 
    } 

    if (!$this->getRequest()->isPost()) { 
     $url = $this->urlModel->getUrl('*/*/create', ['_secure' => true]); 
     $resultRedirect->setUrl($this->_redirect->error($url)); 
     return $resultRedirect; 
    } 

    $this->session->regenerateId(); 

    try { 
     $address = $this->extractAddress(); 
     $addresses = $address === null ? [] : [$address]; 

     $customer = $this->customerExtractor->extract('customer_account_create', $this->_request); 
     $customer->setAddresses($addresses); 

     //Here is I set the telephone and it is giving an error 
     $customer->setTelephone('1234567890'); 

的Magento /客戶/型號/數據/ Customer.php

public function setTelephone($telephone) 
{ 
    return $this->setData(self::TELEPHONE, $telephone); 
} 

public function getTelephone() 
{ 
    return $this->_get(self::TELEPHONE); 
} 

的Magento /客戶/阿比/Data/CustomerInterface.php

<?php 
namespace Magento\Customer\Api\Data; 

interface CustomerInterface extends \Magento\Framework\Api\CustomAttributesDataInterface 
{ 
    /* Add this code*/ 
    const TELEPHONE = 'telephone'; 

    public function getTelephone(); 

    public function setTelephone($telephone); 

} 

試圖做自定義模塊,但它給出了一個錯誤。

1 exception(s): Exception #0 (Magento\Framework\Exception\LocalizedException): Please upgrade your database: Run "bin/magento setup:upgrade" from the Magento root directory. The following modules are outdated: Onjection_Customer data: current version - none, required version - 1.0.0...

模塊代碼

  1. 應用程序/代碼/ Onjection /客戶/和registration.php

    <?php 
    
        \Magento\Framework\Component\ComponentRegistrar::register(
         \Magento\Framework\Component\ComponentRegistrar::MODULE, 
         'Onjection_Customer', 
         __DIR__ 
        ); 
    

2.app/code/Onjection/Customer /etc/module.xml

​​

3.app/code/Onjection/Customer/Setup/InstallData.php

<?php 

namespace Onjection\Customer\Setup; 

use Magento\Framework\Setup\InstallDataInterface; 
use Magento\Framework\Setup\ModuleDataSetupInterface; 
use Magento\Framework\Setup\ModuleContextInterface; 

/** 
* @codeCoverageIgnore 
*/ 
class InstallData implements InstallDataInterface 
{ 
    protected $customerSetupFactory; 

    /** 
    * @var AttributeSetFactory 
    */ 
    private $attributeSetFactory; 

    /** 
    * @param CustomerSetupFactory $customerSetupFactory 
    * @param AttributeSetFactory $attributeSetFactory 
    */ 
    public function __construct(
     CustomerSetupFactory $customerSetupFactory, 
     AttributeSetFactory $attributeSetFactory 
    ) { 
     $this->customerSetupFactory = $customerSetupFactory; 
     $this->attributeSetFactory = $attributeSetFactory; 
    } 

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) 
    { 
     $setup->startSetup(); 

     /** @var CustomerSetup $customerSetup */ 
     $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); 

     $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer'); 
     $attributeSetId = $customerEntity->getDefaultAttributeSetId(); 

     /** @var $attributeSet AttributeSet */ 
     $attributeSet = $this->attributeSetFactory->create(); 
     $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); 

     $customerSetup->addAttribute(Customer::ENTITY, 'mobile', [ 
      'type' => 'varchar', 
      'label' => 'Mobile', 
      'input' => 'text', 
      'required' => false, 
      'visible' => true, 
      'user_defined' => true, 
      'sort_order' => 1000, 
      'position' => 1000, 
      'system' => 0, 
     ]); 

     $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'mobile') 
     ->addData([ 
      'attribute_set_id' => $attributeSetId, 
      'attribute_group_id' => $attributeGroupId, 
      'used_in_forms' => ['customer_address_edit'], 
     ]); 

     $attribute->save(); 
     $setup->endSetup(); 
    } 
} 

命令用於安裝:

  1. PHP斌/ Magento的設置:升級

  2. php bin/magento setup:static-content:deploy

+0

這不是使用Magento的正確方法。見[這裏](https://magento.stackexchange.com/questions/88245/magento2-create-a-customer-custom-attribute)。你不應該修改核心文件。 – subroutines

+0

@subroutines如何在客戶註冊頁面添加這個自定義屬性?他們的任何方法都可以在不創建模塊的情況下執行此操作? – Tomas

+0

'telephone'已經是一個現有的客戶地址屬性,您可以查看定製'magento \ module-customer \ view \ frontend \ templates \ form \ register.phtml'以在註冊頁面上顯示它。 – subroutines

回答

2

可以創建自定義的客戶屬​​性 添加客戶屬性

1)創建模塊文件

<?xml version="1.0" encoding="UTF-8"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> 
    <module name="Kalpesh_Mobile" setup_version="1.0.0"> 
     <sequence>   
      <!--<module name="Kalpesh_Mobile"/>--> 
      <module name="Magento_Customer"/> 
     </sequence> 
    </module> 
</config> 



class InstallData implements InstallDataInterface 
    { 
     protected $customerSetupFactory; 

     /** 
     * @var AttributeSetFactory 
     */ 
     private $attributeSetFactory; 

     /** 
     * @param CustomerSetupFactory $customerSetupFactory 
     * @param AttributeSetFactory $attributeSetFactory 
     */ 
     public function __construct(
      CustomerSetupFactory $customerSetupFactory, 
      AttributeSetFactory $attributeSetFactory 
     ) { 
      $this->customerSetupFactory = $customerSetupFactory; 
      $this->attributeSetFactory = $attributeSetFactory; 
     } 

     public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) 
     { 
        $setup->startSetup(); 


      /** @var CustomerSetup $customerSetup */ 
      $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); 

      $customerEntity = $customerSetup->getEavConfig()- 
     >getEntityType('customer'); 
      $attributeSetId = $customerEntity->getDefaultAttributeSetId(); 

      /** @var $attributeSet AttributeSet */ 
      $attributeSet = $this->attributeSetFactory->create(); 
      $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); 

      $customerSetup->addAttribute(Customer::ENTITY, 'mobile', [ 
       'type' => 'varchar', 
       'label' => 'Mobile', 
       'input' => 'text', 
       'required' => false, 
       'visible' => true, 
       'user_defined' => true, 
       'sort_order' => 1000, 
       'position' => 1000, 
       'system' => 0, 
      ]); 

      $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'mobile') 
      ->addData([ 
       'attribute_set_id' => $attributeSetId, 
       'attribute_group_id' => $attributeGroupId, 
       'used_in_forms' => ['customer_address_edit'], 
      ]); 

      $attribute->save(); 

        $setup->endSetup(); 



     } 
    } 
+0

給出一個錯誤。更新我的問題,請檢查。 – Tomas

2

您需要創建自己的模塊

在Magento單模例如,您可以檢查https://github.com/magento/magento2-samples/blob/master/sample-module-form-uicomponent/view/adminhtml/ui_component/sampleform_form.xml他們提供的功能在M2中添加一個新的領域

<field name="color"> 
    <argument name="data" xsi:type="array"> 
     <item name="config" xsi:type="array"> 
      <!--component constructor--> 
      <item name="component" xsi:type="string">Magento_SampleForm/js/form/element/color-select</item> 
      <!--main template for form field that renders elementTmpl as a child template--> 
      <item name="template" xsi:type="string">ui/form/field</item> 
      <!--customized form element template that will show colors--> 
      <item name="elementTmpl" xsi:type="string">Magento_SampleForm/form/element/color-select</item> 
      <item name="label" xsi:type="string">Autumn colors</item> 
      <item name="visible" xsi:type="boolean">true</item> 
      <item name="dataType" xsi:type="string">text</item> 
      <item name="formElement" xsi:type="string">input</item> 
      <item name="source" xsi:type="string">sampleform</item> 
     </item> 
    </argument> 
</field> 

沒有必要修改MySQL的直接行或更改核心代碼,您可以設想並重寫的所有內容。閱讀與Magento的工作的一般原則文檔2

,並在評論中提到,如果您需要電話領域,它已經實現

相關問題