将自定义列添加到customer_entity

时间:2016-02-15 15:04:15

标签: magento2

我正在尝试向customer_entity添加一个自定义列,该列应该可以在后端以客户身份进行编辑。

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

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

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

  • 我使用UpdateDataScript添加了一个具有相同名称(=列名称)的属性,$ customerSetup-> addAttribute()...
  • 在保存用户时正确更新了Customer_grid_flat,但表customer_entity中的值未更改。它将其值保存在属性表(customer_entity_varchar)中。

如何正确设置自定义列,以便将其值保存在“customer_entity”内而不是“customer_entity_varchar”中?

1 个答案:

答案 0 :(得分:4)

解决方案: 我的自定义属性现在可以正确保存在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();


    }
}

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