Bug #45272

Related Value Objects get deleted by default cascading

Added by Peter Rauber over 8 years ago.

Status:
New
Priority:
Should have
Assignee:
-
Category:
-
Target version:
-
Start date:
2013-02-08
Due date:
% Done:

0%

Estimated time:
PHP Version:
Has patch:
No
Complexity:

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'.

No data to display

Also available in: Atom PDF