moduleDataSetup->getConnection()->startSetup(); /** @var CustomerSetup $customerSetup */ $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]); $customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY); $attributeSetId = $customerEntity->getDefaultAttributeSetId(); $attributeSet = $this->attributeSetFactory->create(); $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); // 检查wechat_avatar属性是否已存在 $attribute = $customerSetup->getAttribute( \Magento\Customer\Model\Customer::ENTITY, 'wechat_avatar' ); if (!$attribute) { // 添加wechat_avatar属性 $customerSetup->addAttribute( \Magento\Customer\Model\Customer::ENTITY, 'wechat_avatar', [ 'type' => 'varchar', 'label' => '微信头像', 'input' => 'text', 'required' => false, 'visible' => false, 'user_defined' => true, 'position' => 1000, 'system' => 0, 'is_used_in_grid' => true, 'is_visible_in_grid' => false, 'is_filterable_in_grid' => true, //建立普通索引 (INDEX) //'is_searchable_in_grid' => true, //将生成 FULLTEXT 索引 ] ); // 将属性添加到表单 $attribute = $customerSetup->getEavConfig()->getAttribute( \Magento\Customer\Model\Customer::ENTITY, 'wechat_avatar' ); $attribute->addData([ 'used_in_forms' => [ 'adminhtml_customer', 'customer_account_create', 'customer_account_edit', ] ]); $attribute->addData([ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId ]); $attribute->save(); } $this->moduleDataSetup->getConnection()->endSetup(); } /** * {@inheritdoc} */ public static function getDependencies() { return [ AddWechatOpenidAttribute::class ]; } /** * {@inheritdoc} */ public function getAliases() { return []; } /** * {@inheritdoc} */ public function revert() { $this->moduleDataSetup->getConnection()->startSetup(); /** @var CustomerSetup $customerSetup */ $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]); // 删除wechat_avatar属性 $customerSetup->removeAttribute( \Magento\Customer\Model\Customer::ENTITY, 'wechat_avatar' ); $this->moduleDataSetup->getConnection()->endSetup(); } }