Magento2 向customer\u实体添加自定义列

Magento2 向customer\u实体添加自定义列,magento2,Magento2,我正在尝试向customer_实体添加一个自定义列,该列在后端的customer表单中应该是可编辑的 我可以通过模块中的UpdateSchema脚本将列添加到数据库表中 但是如何在客户表单和网格中填充它呢 到目前为止,我尝试的是: 我添加了一个与UpdateDataScript、$customerSetup->addAttribute()同名(=列名)的属性 保存用户时,Customer\u grid\u flat已正确更新,但表Customer\u实体中的值未更改。它将其值保存在属性表(cu

我正在尝试向customer_实体添加一个自定义列,该列在后端的customer表单中应该是可编辑的

我可以通过模块中的UpdateSchema脚本将列添加到数据库表中

但是如何在客户表单和网格中填充它呢

到目前为止,我尝试的是:

  • 我添加了一个与UpdateDataScript、$customerSetup->addAttribute()同名(=列名)的属性
  • 保存用户时,Customer\u grid\u flat已正确更新,但表Customer\u实体中的值未更改。它将其值保存在属性表(customer\u entity\u varchar)中
如何正确设置自定义列,使其值保存在“customer\u entity”中,而不是保存在“customer\u entity\u varchar”中?解决方案: 我的自定义属性现在正确地保存在customer_entity表中

UpgradeSchema.php

<?php

namespace Custom\MyModule\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{
    const CUSTOM_ATTRIBUTE_ID = 'custom_attribute';
    /**
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '0.0.3', '<')) {
            $setup->getConnection()->addColumn(
                $setup->getTable('customer_entity'),
                self::CUSTOM_ATTRIBUTE_ID,
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'nullable' => true,
                    'default' => null,
                    'comment' => 'Custom Attribute'
                ]
            );
        }

        $setup->endSetup();


    }
}
<?php

namespace Custom\MyModule\Setup;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Indexer\IndexerRegistry;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\TestFramework\Helper\Eav;

class UpgradeData implements UpgradeDataInterface
{
    /**
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * @var IndexerRegistry
     */
    protected $indexerRegistry;

    /**
     * @var \Magento\Eav\Model\Config
     */
    protected $eavConfig;

    /**
     * @var \Magento\Eav\Model\Setup
     */
    protected $eavSetupFactory;

    /**
     * @param CustomerSetupFactory $customerSetupFactory
     * @param IndexerRegistry $indexerRegistry
     * @param \Magento\Eav\Model\Config $eavConfig
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        IndexerRegistry $indexerRegistry,
        \Magento\Eav\Model\Config $eavConfig,
        EavSetupFactory $eavSetupFactory
    )
    {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->indexerRegistry = $indexerRegistry;
        $this->eavConfig = $eavConfig;
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * Upgrades data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $dbVersion = $context->getVersion();

        if (version_compare($dbVersion, '0.0.3', '<')) {

            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

            $customerSetup->addAttribute(
                'customer',
                UpgradeSchema::CUSTOM_ATTRIBUTE_CODE,
                [
                    'label' => 'Custom Attribute',
                    'required' => 0,
                    'visible' => 1, //<-- important, to display the attribute in customer edit
                    'input' => 'text',
                    'type' => 'static',
                    'system' => 0, // <-- important, to have the value be saved
                    'position' => 40,
                    'sort_order' => 40
                ]
            );

            /** @var  EavSetupFactory $eavSetup */
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            $typeId = $eavSetup->getEntityTypeId('customer');

            $attribute = $eavSetup->getAttribute($typeId, UpgradeSchema::CUSTOM_ATTRIBUTE_ID);

            $customerSetup->getSetup()->getConnection()->insertMultiple(
                $customerSetup->getSetup()->getTable('customer_form_attribute'),
                array('form_code' => 'adminhtml_customer', 'attribute_id' => $attribute['attribute_id'])
            );

            $setup->endSetup();
        }
    }
}