2

语境

我正在尝试更改核心实体之一上一个字段的表单类型:Business Unit

默认表单域是TextField,我想将其更改为ChoiceType.

这是我在使用迁移创建的业务单位实体上的自定义字段:

$table->addColumn('periodicite', 'string', [
    'oro_options' => [
        'extend' => ['owner' => ExtendScope::OWNER_CUSTOM],
        'entity' => ['label' => 'Périodicité'],
    ],
]);

问题

我在 Oro 文档中看到entity_config.yml可以解决我的问题。我试图把这些线,但它不工作:

entity_config:
    business_unit:
          entity:
              items:
                  periodicite:
                      form:
                          type: Symfony\Component\Form\Extension\Core\Type\ChoiceType
                          options:
                            choices:
                              Mensuel: Mensuel
                              Trimestriel: Trimestriel
                            placeholder: false
                            required: true
                            label: "Périodicite"

我还尝试创建一个新的迁移来更改我的自定义字段上的字段类型,但它不起作用

<?php

namespace Baltimore\Bundle\AppBundle\Migrations\Schema\v1_1;

use Doctrine\DBAL\Schema\Schema;
use Oro\Bundle\EntityConfigBundle\Migration\UpdateEntityConfigFieldValueQuery;
use Oro\Bundle\EntityExtendBundle\EntityConfig\ExtendScope;
use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtension;
use Oro\Bundle\EntityExtendBundle\Migration\Extension\ExtendExtensionAwareInterface;
use Oro\Bundle\MigrationBundle\Migration\Migration;
use Oro\Bundle\MigrationBundle\Migration\QueryBag;
use Oro\Bundle\OrganizationBundle\Entity\BusinessUnit;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

class UpdateBusinessUnitField implements Migration, ExtendExtensionAwareInterface
{
    /** @var ExtendExtension */
    protected $extendExtension;

    /**
     * @inheritdoc
     */
    public function setExtendExtension(ExtendExtension $extendExtension)
    {
        $this->extendExtension = $extendExtension;
    }

    public function up(Schema $schema, QueryBag $queries)
    {
        $queries->addQuery(
            new UpdateEntityConfigFieldValueQuery(
                BusinessUnit::class,
                'periodicite',
                'form',
                'form_type',
                ChoiceType::class
            )
        );

        $queries->addQuery(
            new UpdateEntityConfigFieldValueQuery(
                BusinessUnit::class,
                'periodicite',
                'form',
                'form_options',
                [
                    'choices' => [
                        'Mensuel' => 'Mensuel',
                        'Trimestriel' => 'Trimestriel',
                        'Annuel' => 'Annuel',
                    ],
                ]
            )
        );
    }
}
4

2 回答 2

2

我在我的迁移文件中找到了该changeColumn方法的解决方案,它就像一个魅力。

顺便说一句,这些属性也适用于该addColumn方法。

public function up(Schema $schema, QueryBag $queries)
{
    $table = $schema->getTable('oro_business_unit');

    $table->changeColumn('periodicite', [
        'oro_options' => [
            'extend' => ['owner' => ExtendScope::OWNER_CUSTOM],
            'entity' => ['label' => 'Périodicité'],
            'form' => [
                'form_type' => ChoiceType::class,
                'form_options' => [
                    'choices' => [
                        'Mensuel' => 'Mensuel',
                        'Trimestriel' => 'Trimestriel',
                        'Semestriel' => 'Semestriel',
                        'Annuel' => 'Annuel'
                    ]
                ]
            ],
        ],
    ]);
}
于 2021-02-23T21:44:52.903 回答
1

我不知道使用 YAML 文件覆盖实体配置元数据的可能性。如果有 - 请在评论中分享您用于实现它的文档。

但是可以肯定的是,您可以使用架构迁移来管理相同的内容,如下例所示:

class UpdateOpportunityRelationFormType implements Migration
{
    /**
     * {@inheritdoc}
     */
    public function up(Schema $schema, QueryBag $queries)
    {
        $queries->addQuery(
            new UpdateEntityConfigFieldValueQuery(
                Quote::class,
                'opportunity',
                'form',
                'form_type',
                OpportunitySelectType::class
            )
        );

        $queries->addQuery(
            new UpdateEntityConfigFieldValueQuery(
                Quote::class,
                'opportunity',
                'form',
                'form_options',
                ['attr' => ['readonly' => true]]
            )
        );
    }
}
于 2021-02-22T17:25:26.803 回答