2017-05-11 16 views
7

मैं ग्राहक तालिका में magento2 में एक कस्टम कॉलम (टेलीफोन) जोड़ना चाहता हूं और ग्राहक पंजीकरण के दौरान इस क्षेत्र में मूल्य जोड़ना चाहता हूं।Magento2 में ग्राहक तालिका में कस्टम कॉलम कैसे जोड़ें?

इसके लिए सबसे पहले मैं customer_entity table.While में डीबी में एक स्तंभ (टेलीफोन) बनाने के ग्राहक जब मैं $ ग्राहक फोन> ('1234567890') Magento/ग्राहक/नियंत्रक/खाता/CreatePost में बनाने setTelephone। कार्यान्वयन समारोह में PHP। यह एक त्रुटि दे रहा है मैगेंटो/ग्राहक/मॉडल/डेटा/ग्राहक.पीपी में फ़ंक्शन सेट टेलेफ़ोन को परिभाषित करें। लेकिन मैं इस मॉडल में पहले ही इस समारोह को बना रहा हूं।

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

<?xml version="1.0"?> 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> 
    <module name="Onjection_Customer" setup_version="1.0.0"> 
     <sequence>   
      <module name="Magento_Customer"/> 
     </sequence> 
    </module> 
</config> 

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 bin/Magento स्थापना: उन्नयन

  2. php bin/magento सेटअप: स्थैतिक-सामग्री: तैनात

+0

Magento के साथ काम करने का यह सही तरीका नहीं है। [यहां] देखें (https://magento.stackexchange.com/questions/88245/magento2-create-a- ग्राहक- कस्टम- सामग्री)। आपको कोर फाइलों को भी संशोधित नहीं करना चाहिए। – subroutines

+0

@subroutines ग्राहक पंजीकरण पृष्ठ पर यह कस्टम विशेषता कैसे जोड़ें? क्या मॉड्यूल बनाने के बिना ऐसा करने का कोई तरीका है? – Tomas

+0

'टेलीफोन' पहले से ही एक मौजूदा ग्राहक पता विशेषता है, आप इसे पंजीकरण पृष्ठ पर दिखाने के लिए' 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 वे एम 2 में एक नए क्षेत्र

<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

और के रूप में टिप्पणी में उल्लेख किया है, तो आप टेलीफोन क्षेत्र की जरूरत है यह पहले से ही लागू है

संबंधित मुद्दे