Bug #45272
Related Value Objects get deleted by default cascading
| Status: | New | Start date: | 2013-02-08 | |
|---|---|---|---|---|
| Priority: | Should have | Due date: | ||
| Assigned To: | - | % Done: | 0% |
|
| Category: | - | |||
| Target version: | - | |||
| PHP Version: | Complexity: | |||
| Has patch: | No | Affected Flow version: | (any) |
Description
Description by example:
An entity that relates to a value object:
/**
* A Building
*
* @FLOW3\Entity
*/
class Building {
/**
* @ORM\ManyToOne
* @var \MyNamespace\Domain\Model\BuildingType
*/
protected $buildingType;
}
Two entities point to the same BuildingType:
$building1 = new \MyNamespace\Domain\Model\Building();
$building1->setBuildingType(new \MyNamespace\Domain\Model\BuildingType('Type 1'));
$this->buildingRepository->add($building1);
$building2 = new \MyNamespace\Domain\Model\Building();
$building2->setBuildingType(new \MyNamespace\Domain\Model\BuildingType('Type 1'));
$this->buildingRepository->add($building2);
When you try to delete building1, FLOW/doctrine will cascade the removal to the value object and that will cause an exception, because building2 still needs the value object, as it points to it's identifier.
I solved that by adding an empty cascade-attribute to that relations: @ORM\ManyToOne(cascade={""}).
Suggestion:
The cascading was added by Flow3AnnotationDriver as this driver adds $mapping['cascade'] = array('all') to all properties that are not aggregate roots. I suggest that in the case of value objects $mapping['cascade'] = array('persist') will be added istead of 'all'.