Magento2 自定义属性显示但未保存在magento 2管理员客户创建表单中

Magento2 自定义属性显示但未保存在magento 2管理员客户创建表单中,magento2,custom-attributes,Magento2,Custom Attributes,我已在magento 2环境中创建了新的客户属性 字段已添加该字段中的数据未保存。由于保存数据时出错,我收到错误消息。我找不到添加新属性的好教程。请提供帮助 我遵循了这个准则 数据保存不会自动进行,您需要将数据设置为属性。 如果它是客户界面的一部分 比如说 $attribute = $customer->getCustomAttribute('client_dn'); if ($attribute) { $customer->setValue("hi"); } if you

我已在magento 2环境中创建了新的客户属性

字段已添加该字段中的数据未保存。由于保存数据时出错,我收到错误消息。我找不到添加新属性的好教程。请提供帮助

我遵循了这个准则


数据保存不会自动进行,您需要将数据设置为属性。 如果它是客户界面的一部分 比如说

$attribute = $customer->getCustomAttribute('client_dn');
if ($attribute)
{
    $customer->setValue("hi");
}

if you are saving using customer object
$customer->setData('client_dn', 'Hi');

数据保存本身不会发生,您需要将数据设置为属性。 如果它是客户界面的一部分 比如说

$attribute = $customer->getCustomAttribute('client_dn');
if ($attribute)
{
    $customer->setValue("hi");
}

if you are saving using customer object
$customer->setData('client_dn', 'Hi');

客户注册自定义属性值在管理和保存中创建

1。文本字段

2。下拉字段

3。日期字段

使用
UpgradeSchema.php

<?php

namespace {CompanyName}\{ModuleName}\Setup;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
/* irrelevant */
#use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
/* irrelevant */
#use Magento\Framework\Setup\SchemaSetupInterface;
/* add this */
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements  UpgradeDataInterface
{
    private $customerSetupFactory;

    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '1.0.1', '<')) 
        {
            // For Text field
            $customerSetup->addAttribute(
                    \Magento\Customer\Model\Customer::ENTITY,
                    'attribute_title',                    
                    [
                        'type' => 'text',
                        'input' => 'text',
                        'label' => 'Attribute Title',
                        'required' => false,
                        'visible' => true,
                        'user_defined' => false,
                        'sort_order' => 1000,
                        'position' => 1000,
                        'system' => 0,
                    ]
                );
            $attribute_title = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'attribute_title')
                ->addData(
                    ['used_in_forms' => ['adminhtml_customer']
                ]);

            $attribute_title->save();

            //Add field Drop Down for Yes/No
            $customerSetup->addAttribute(
                    \Magento\Customer\Model\Customer::ENTITY,
                    'is_attribute',                    
                    [
                        'type' => 'int',
                        'input' => 'select',
                        'label' => 'Is Attribute',
                        'frontend' => '',
                        'default' => '1',
                        'class' => '',
                        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                        'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                        'required' => false,
                        'visible' => true,
                        'user_defined' => false,
                        'sort_order' => 1000,
                        'position' => 1000,
                        'system' => 0,
                    ]
                );
            $is_attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'is_attribute')
                ->addData(
                    ['used_in_forms' => ['adminhtml_customer']
                ]);

            $is_attribute->save();

            // For Date And Time field
            $customerSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                'custom_date',                    
                [
                    'label' => 'Custom Date',
                    'type' => 'datetime',
                    'input' => 'date',
                    'frontend' => 'Magento\Eav\Model\Entity\Attribute\Frontend\Datetime',
                    'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\Datetime',
                    'validate_rules' => '{"input_validation":"date"}',
                    'user_defined' => false,
                    'required' => false,
                    'visible' => true,
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'sort_order' => 1000,
                    'position' => 1000,
                    'system' => 0,
                ]
            );
            $custom_date = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_date')
                ->addData(
                    ['used_in_forms' => ['adminhtml_customer']
                ]);
                // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
            $commenced_business->save();
        }
    }
}

在管理和保存中创建客户注册自定义属性值

1。文本字段

2。下拉字段

3。日期字段

使用
UpgradeSchema.php

<?php

namespace {CompanyName}\{ModuleName}\Setup;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetup;
/* irrelevant */
#use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
/* irrelevant */
#use Magento\Framework\Setup\SchemaSetupInterface;
/* add this */
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements  UpgradeDataInterface
{
    private $customerSetupFactory;

    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '1.0.1', '<')) 
        {
            // For Text field
            $customerSetup->addAttribute(
                    \Magento\Customer\Model\Customer::ENTITY,
                    'attribute_title',                    
                    [
                        'type' => 'text',
                        'input' => 'text',
                        'label' => 'Attribute Title',
                        'required' => false,
                        'visible' => true,
                        'user_defined' => false,
                        'sort_order' => 1000,
                        'position' => 1000,
                        'system' => 0,
                    ]
                );
            $attribute_title = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'attribute_title')
                ->addData(
                    ['used_in_forms' => ['adminhtml_customer']
                ]);

            $attribute_title->save();

            //Add field Drop Down for Yes/No
            $customerSetup->addAttribute(
                    \Magento\Customer\Model\Customer::ENTITY,
                    'is_attribute',                    
                    [
                        'type' => 'int',
                        'input' => 'select',
                        'label' => 'Is Attribute',
                        'frontend' => '',
                        'default' => '1',
                        'class' => '',
                        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                        'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                        'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                        'required' => false,
                        'visible' => true,
                        'user_defined' => false,
                        'sort_order' => 1000,
                        'position' => 1000,
                        'system' => 0,
                    ]
                );
            $is_attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'is_attribute')
                ->addData(
                    ['used_in_forms' => ['adminhtml_customer']
                ]);

            $is_attribute->save();

            // For Date And Time field
            $customerSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                'custom_date',                    
                [
                    'label' => 'Custom Date',
                    'type' => 'datetime',
                    'input' => 'date',
                    'frontend' => 'Magento\Eav\Model\Entity\Attribute\Frontend\Datetime',
                    'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\Datetime',
                    'validate_rules' => '{"input_validation":"date"}',
                    'user_defined' => false,
                    'required' => false,
                    'visible' => true,
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'sort_order' => 1000,
                    'position' => 1000,
                    'system' => 0,
                ]
            );
            $custom_date = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'custom_date')
                ->addData(
                    ['used_in_forms' => ['adminhtml_customer']
                ]);
                // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
            $commenced_business->save();
        }
    }
}