【问题标题】:Symfony 2 - How to allow a entities variable which is an object of another entity to be null?Symfony 2 - 如何允许作为另一个实体对象的实体变量为空?
【发布时间】:2017-01-03 13:48:13
【问题描述】:

假设我有一个实体类Travel

<?php

namespace Bundle\TravelCostsBundle\Entity;

class Travel {

     ...

    /**
     *
     * @var \Bundle\TravelCostsBundle\Entity\MilageAllowance
     */
    private $milageAllowance;

    ...

    /**
     * Set milageAllowance
     *
     * @param \Bundle\TravelCostsBundle\Entity\MilageAllowance $milageAllowance         
     *
     * @return Travel
     */
    public function setMilageAllowance(\Bundle\TravelCostsBundle\Entity\MilageAllowance $milageAllowance = null) {
        $this->milageAllowance = $milageAllowance;

        return $this;
    }

    /**
     * Get milageAllowance
     *
     * @return \Bundle\TravelCostsBundle\Entity\MilageAllowance
     */
    public function getMilageAllowance() {
        return $this->milageAllowance;
    }

    ...
}

这是 Doctrine .yml 文件:

# src/TravelCostsBundle/Resources/config/doctrine/Travel.orm.yml
Pec\Bundle\TravelCostsBundle\Entity\Travel:
  type: entity
  repositoryClass: Pec\Bundle\TravelCostsBundle\Repository\TravelRepository
  id:
    id:
      type: integer
      generator: {strategy: AUTO}
  ...
  oneToOne:
    milageAllowance:
      targetEntity: MilageAllowance
      mappedBy: travel
  fields:
    ...

应用程序使用 Doctrine 与数据库连接。我使用validation.yml-文件验证实体,并有一个表单来创建Travel 类型的实体,其中包含milageAllowance-Entity 的变量字段。

我希望填写这些字段是可选的...

  • 如果字段为空$milageAllowance 保持NULL 并且(更重要的是)不会在数据库中创建条目
    • 如果字段已填写,将在数据库中创建一个条目。

在 oneToOne 关系中这是否可能?

如何验证实体Travel 可以只有1 个或没有milageAllowance

感谢您的帮助...如有不清楚的地方请询问!

【问题讨论】:

  • 您希望如何完成此任务?在同一个表单中添加多个millageAllowance,并在提交时将此millageAllowance附加到旅行实体?

标签: php symfony doctrine-orm symfony-2.8


【解决方案1】:

您可以通过设置 JoinColumn(nullable=true) 使您的 OneToOne 关系可选

使用 YML 表示法,它看起来像这样:

    ...
  oneToOne:
    milageAllowance:
      targetEntity: MilageAllowance
      mappedBy: travel
  JoinColumn
    nullable: true 
  fields:
    ...

查看此页面了解更多信息:

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html

【讨论】:

  • 好的,谢谢,现在我有一个表单,其中包含 milageAllowance 的字段和 validation.yml 内的 milageAllowance 的验证规则。如果我提交基本表单,验证是否会出错,或者我是否必须明确告诉 Symfony 某个字段可以保持为空?
  • 如果您在该字段上有 NotBlank 或 NotNull 之类的断言 - 我认为您应该删除它们以使事情正常运行,如果不是 - 一切都应该没问题
  • 抱歉我的回复晚了。我阅读了文章How to Embed a Collection of Forms,其中也不包括对任意数量的实体对象的实体验证。但我认为这或多或少有些混乱,因为 Symfony 中的简单验证可能性可以使用它们。有没有办法添加仅在我希望实体对象获取值时才使用的验证约束?稍后我可能会在我的问题中添加一些代码以更准确地显示我的意思......
【解决方案2】:

感谢Stas Parshin,它给了我部分答案。所以在orm文件中设置nullable: true

Travel.orm.yml:

Projekt\Bundle\MyBundle\Entity\Travel:
  oneToOne:
    milageAllowance:
      targetEntity: MilageAllowance
      mappedBy: travel
      JoinColumn:
        nullable: true

TravelType 类中,将添加的ReceiptType 表单设置为'required' =&gt; false

class TravelType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
            ->add('milageAllowance', MilageAllowanceType::class, array(
                'required'      => false,
            //Are all fields of milageAllowance empty => $travel->milageAllowance == NULL
                ...
        ))

如果所有字段都属于ReceiptType,则将属性$travel-&gt;milageAllowance == NULL留空。

【讨论】:

    猜你喜欢
    • 2015-02-04
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多