【发布时间】: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