如何在OroPlatform中实现Select Type并扩展customer user实体添加选择字段
OroPlatform 扩展Customer User实体新增Select类型字段实现步骤
1. 编写迁移文件新增实体字段
首先创建数据库迁移文件,给Customer User实体新增存储Select选项值的字段,示例代码如下:
<?php namespace Your\Bundle\Migrations\Schema\v1_0; use Doctrine\DBAL\Schema\Schema; use Oro\Bundle\MigrationBundle\Migration\Migration; use Oro\Bundle\MigrationBundle\Migration\QueryBag; class AddCustomSelectField implements Migration { public function up(Schema $schema, QueryBag $queries) { $table = $schema->getTable('oro_customer_user'); // 字段类型根据你的选项值调整,用string存文本值或者int存枚举值均可 $table->addColumn('custom_user_type', 'string', [ 'length' => 50, 'notnull' => false, 'comment' => '自定义用户类型(下拉选择字段)' ]); } }
执行迁移命令生效:php bin/console oro:migration:load --force
2. 配置表单字段为Select下拉类型
通过FormTypeExtension扩展CustomerUser的编辑表单,将新增字段设置为选择类型,示例代码如下:
<?php namespace Your\Bundle\Form\Extension; use Oro\Bundle\CustomerBundle\Form\Type\CustomerUserType; use Symfony\Component\Form\AbstractTypeExtension; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\FormBuilderInterface; class CustomerUserTypeExtension extends AbstractTypeExtension { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('custom_user_type', ChoiceType::class, [ 'label' => '自定义用户类型', 'choices' => [ '普通用户' => 'normal', 'VIP用户' => 'vip', '企业用户' => 'enterprise' ], 'required' => false, // 配置Oro字段权限控制 'oro_field_acl' => true ]); } public static function getExtendedTypes(): iterable { return [CustomerUserType::class]; } }
如果需要选项可在后台动态管理,不需要硬编码,可单独创建一个选项配置实体,将ChoiceType替换为EntityType关联对应的配置实体即可
3. 配置后台列表/详情页展示
扩展Customer User的数据网格配置,将字段存储值转换为对应的选项文本展示,在你的Bundle的config/oro/datagrids.yml中添加如下配置:
datagrid: oro_customer_user_grid: columns: custom_user_type: label: 自定义用户类型 type: choice choices: normal: 普通用户 vip: VIP用户 enterprise: 企业用户 filters: columns: custom_user_type: type: choice options: field_options: choices: 普通用户: normal VIP用户: vip 企业用户: enterprise
4. 权限配置
进入Oro后台的「系统」-「用户管理」-「角色」,给需要操作该字段的角色开启custom_user_type字段的查看、编辑权限,清空缓存后即可正常使用。
注意事项
- 如果需要该字段在前台店面对客展示,可直接在对应Twig模板中通过字段值匹配输出对应选项文本
- 所有配置修改完成后需执行
php bin/console cache:clear清空缓存生效
内容的提问来源于stack exchange,提问作者Reita
相关产品推荐
相关产品推荐

