Symfony 2 - 如何允许作为另一个实体对象的实体变量为空?
Symfony 2 - How to allow a entities variable which is an object of another entity to be null?
所以假设我有一个实体-Class 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
-实体变量的字段。
我希望填写字段是可选的,所以...
- 如果字段为空
$milageAllowance
保持不变 NULL
并且(更重要的是)不会在数据库中创建任何条目
- 如果填写了字段,将在数据库中创建一个条目。
一对一关系甚至可能吗?
如何验证实体 Travel
可以只有 1 个或没有 milageAllowance
?
感谢您的帮助...如果有任何不清楚的地方,请询问!
您可以通过设置 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
感谢 Stas Parshin,它给了我一部分答案。所以在orm
文件中设置nullable: true
:
Travel.orm.yml
:
Projekt\Bundle\MyBundle\Entity\Travel:
oneToOne:
milageAllowance:
targetEntity: MilageAllowance
mappedBy: travel
JoinColumn:
nullable: true
在 TravelType
class 中将添加的 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
就像注释一样
所以假设我有一个实体-Class 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
-实体变量的字段。
我希望填写字段是可选的,所以...
- 如果字段为空
$milageAllowance
保持不变NULL
并且(更重要的是)不会在数据库中创建任何条目- 如果填写了字段,将在数据库中创建一个条目。
一对一关系甚至可能吗?
如何验证实体 Travel
可以只有 1 个或没有 milageAllowance
?
感谢您的帮助...如果有任何不清楚的地方,请询问!
您可以通过设置 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
感谢 Stas Parshin,它给了我一部分答案。所以在orm
文件中设置nullable: true
:
Travel.orm.yml
:
Projekt\Bundle\MyBundle\Entity\Travel:
oneToOne:
milageAllowance:
targetEntity: MilageAllowance
mappedBy: travel
JoinColumn:
nullable: true
在 TravelType
class 中将添加的 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