将自定义列添加到 customer_entity

Adding custom column to customer_entity

我正在尝试向 customer_entity 添加一个自定义列,它应该是后端客户表单中的 editable。

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

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

到目前为止我尝试了什么:

如何正确设置自定义列,使其值保存在 'customer_entity' 而不是 'customer_entity_varchar' 中?

解决方法: 我的自定义属性现在可以正确保存在 customer_entity table.

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


    }
}

UpgradeData.php

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