So lets say I have an Entity-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;
}
...
}
Here is the Doctrine .yml-file:
# 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:
...
The Application is connected with a database using Doctrine. I validate the Entities with a validation.yml-file and have a Form to create an Entity of Type Travel with fields for the variables of the milageAllowance-Entity.
I want that it is optional to fill the fields so...
- if the fields are empty
$milageAllowancestaysNULLand (far more important) NO entry in the database will be created- if the fields are filled an entry in the database will be created.
Is that even possible with a oneToOne-relation?
How to validate that the Entity Travel can have just 1 or no milageAllowance?
I appreciate your help... Please ask if anything is unclear!
Thanks to Stas Parshin which gives me one part of the answer. So set
nullable: truein theormfile:Travel.orm.yml:Inside the
TravelTypeclass set the addedReceiptTypeform to'required' => false:Like it is commented if all fields are of
ReceiptTypeleft blank the property$travel->milageAllowance == NULL