所以可以說我有Travel
實體級:Symfony 2 - 如何允許作爲另一個實體的對象的實體變量爲空?
<?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;
}
...
}
這裏是教義.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
和(更重要的)將被創建-
NO在數據庫條目
- 如果填充字段,則會創建數據庫中的條目。
是,即使可能有oneToOne-關係?
如何驗證實體Travel
可以只有1個或沒有milageAllowance
?
我很感謝您的幫助...請問是否有什麼不清楚!
如何你想完成這個?在同一表格中添加多個millageAllowance,並在提交時將此millageAllowance附加到旅行實體? –