【问题标题】:OroPlatform: add custom field on core EntityOroPlatform:在核心实体上添加自定义字段
【发布时间】:2021-03-18 05:09:23
【问题描述】:

我目前正在处理一个 OroPlatform 项目,我需要在 BusinessUnit 核心实体上添加一个自定义字段。

我已阅读有关扩展核心实体的方式的 Oro 文档部分:https://doc.oroinc.com/backend/entities/extend-entities/#id1

<?php
namespace MyBundle\Bundle\AppBundle\Migrations\Schema\v1_0;

use Doctrine\DBAL\Schema\Schema;
use Oro\Bundle\EntityExtendBundle\EntityConfig\ExtendScope;
use Oro\Bundle\MigrationBundle\Migration\Migration;
use Oro\Bundle\MigrationBundle\Migration\QueryBag;

class AddColumnsToBusinessUnit implements Migration
{
    public function up(Schema $schema, QueryBag $queries)
    {
        $table = $schema->getTable('oro_business_unit');
        $table->addColumn('siret', 'string', [
            'oro_options' => [
                'extend' => ['owner' => ExtendScope::OWNER_CUSTOM],
                'entity' => ['label' => 'siret'],
            ],
        ]);
    }
}

当我运行命令 symfony console oro:migration:load --force 时,它可以工作,并且迁移将应用于我的数据库。

现在,我想要一个必填字段。我已经看到指令'notnull' =&gt; true 在数据库上设置一个不可为空的字段。

一切正常,但我的字段在 organization/business_unit/create 路由上没有任何 JavaScript 验证。有什么想法吗?

【问题讨论】:

    标签: symfony orocommerce


    【解决方案1】:

    您可以通过扩展已为您要扩展的核心实体定义的验证元数据来验证新字段。

    为此,请遵循 Symfony 官方文档并使用 YML 格式: https://symfony.com/doc/4.4/validation.html#constraint-configuration

    您可以用于该字段的约束是“非空白”。

    这是一个例子:

    # src/<YourBundlePath>/Resources/config/validation.yml
    Oro\Bundle\OrganizationBundle\Entity\BusinessUnit:
        properties:
            siret:
                - NotBlank: ~
    

    【讨论】:

    • 哇,谢谢@Andrey Yatsenko,这绝对是我需要的!将'notnull' =&gt; true 添加到我的迁移文件中是一个好习惯吗?如果是,我如何从默认的 OrganizationBundle 覆盖 LoadOrganizationAndBusinessUnitData.php 夹具?
    • 迁移中的notnull是可以的,只要在代码中处理。您可以通过在原始迁移之后运行另一个依赖迁移并更改先前迁移加载的数据来覆盖迁移。
    猜你喜欢
    • 2021-05-23
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 2018-12-05
    • 1970-01-01
    • 2011-12-18
    相关资源
    最近更新 更多