如何在Magento2的客户表中添加自定义列?

我想在magento2的Customer表中添加一个自定义列(Telephone),并希望在客户注册时在此字段中添加值。

首先,我在customer_entity表中的DB中创建一个列(Telephone)。当我在执行函数中调用Magento / Customer / Controller / Account / CreatePost.php中的 $ customer-> setTelephone('1234567890')时创建客户。 它给出一个错误在Magento / Customer / Model / Data / Customer.php中取消定义函数setTelephone 。 但是我已经在这个模型中创建了这个函数。

Magento的/客户/控制器/帐号/ CreatePost.php

public function execute()
{
    /** @var MagentoFrameworkControllerResultRedirect $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的/客户/原料药/数据/ CustomerInterface.php

<?php
namespace MagentoCustomerApiData;

interface CustomerInterface extends MagentoFrameworkApiCustomAttributesDataInterface
{
    /* Add this code*/
    const TELEPHONE = 'telephone';

    public function getTelephone();

    public function setTelephone($telephone);

}   

试图做自定义模块,但它给出了一个错误。

1 exception(s): Exception #0 (MagentoFrameworkExceptionLocalizedException): 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...

模块代码

  • 应用程序/代码/ Onjection /客户/和registration.php

    <?php
    
        MagentoFrameworkComponentComponentRegistrar::register(
            MagentoFrameworkComponentComponentRegistrar::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 OnjectionCustomerSetup;
    
    use MagentoFrameworkSetupInstallDataInterface;
    use MagentoFrameworkSetupModuleDataSetupInterface;
    use MagentoFrameworkSetupModuleContextInterface;
    
    /**
     * @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();
        }
    }
    

    用于安装的命令:

  • php bin / magento setup:升级

  • php bin / magento setup:static-content:deploy


  • 您可以创建自定义客户属性添加客户属性

    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();
    
    
    
            }
        }
    

    您需要创建自己的模块

    在Magento简单模块示例中您可以检查它们提供的添加功能的https://github.com/magento/magento2-samples/blob/master/sample-module-form-uicomponent/view/adminhtml/ui_component/sampleform_form.xml一个新的领域

    <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>
    

    以m2为单位,不需要直接编辑mysql行或更改核心代码,您可以设想并重写所有内容。 阅读有关使用magento 2的一般原则的文档

    如评论中所述,如果您需要电话,它已经实施

    链接地址: http://www.djcxy.com/p/96333.html

    上一篇: How to add a custom column in customer table in Magento2?

    下一篇: Routing in CodeIgniter for (:any)