Php Symfony 2-如何允许作为另一个实体对象的实体变量为空?

Php Symfony 2-如何允许作为另一个实体对象的实体变量为空?,php,symfony,doctrine-orm,symfony-2.8,Php,Symfony,Doctrine Orm,Symfony 2.8,假设我有一个实体类Travel: <?php namespace Bundle\TravelCostsBundle\Entity; class Travel { ... /** * * @var \Bundle\TravelCostsBundle\Entity\MilageAllowance */ private $milageAllowance; ... /** * Set milageAllo

假设我有一个实体类
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;
    }

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

我希望它是可选的填写字段,以便

  • 如果字段为空
    $milageAllowance
    将保持
    NULL
    ,并且(更重要的是)不会在数据库中创建任何条目
    • 如果字段已填充,将在数据库中创建一个条目
一对一的关系也有可能吗

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


我感谢你的帮助。。。如果有什么不清楚的地方,请询问

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

使用YML表示法,它将如下所示:

    ...
  oneToOne:
    milageAllowance:
      targetEntity: MilageAllowance
      mappedBy: travel
  JoinColumn
    nullable: true 
  fields:
    ...
有关更多信息,请参阅本页:

多亏了它,我得到了部分答案。因此,在
orm
文件中设置
nullable:true

Travel.orm.yml

Projekt\Bundle\MyBundle\Entity\Travel:
  oneToOne:
    milageAllowance:
      targetEntity: MilageAllowance
      mappedBy: travel
      JoinColumn:
        nullable: true
TravelType
类中,将添加的
ReceiptType
表单设置为
'required'=>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->milageAllowance==NULL

您希望如何完成此操作?在同一表单中添加多个millageAllowance,提交时,将此millageAllowance附加到旅行实体?好的,谢谢,现在我有了一个表单,其中包含
milageAllowance
字段和
milageAllowance
的验证规则,位于
validation.yml
内。如果我提交基本表单,验证是否会出错,或者我是否必须明确告诉Symfony某个地方字段可以保持为空?如果您在该字段上有类似NotBlank或NotNull的断言-我想您应该删除它们以使事情正常进行,如果没有-所有人都应该很好,我为我的延迟响应感到抱歉。我读了这篇文章,其中也没有包括对任意数量实体对象的实体验证。但我认为这或多或少有些混乱,因为Symfony中的简单验证可能会用到它们。是否有一种方法可以添加验证约束,这些约束仅在我希望EntityObject获取值时使用?稍后我可能会在我的问题中添加一些代码,以更准确地显示我的意思。。。
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
                ...
        ))