[DDC-2297] Add ConstraintException for parent row Created: 15/Feb/13  Updated: 15/Feb/13  Resolved: 15/Feb/13

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Trivial
Reporter: Daniel Lima Assignee: Benjamin Eberlei
Resolution: Won't Fix Votes: 0
Labels: None


Currently there is no constraint check for entities that depends another one.
I always check the dependencies using repository. Something like this:

$childRepository = $em->getRepository('Child');
$parent = $em->getReference('app:Parent', $id);
$children = $childRepository->findBy(array('parent' => $parent));

if ($children) {
    throw new Exception('You can not delete this record. There are records related');

This exception could be thrown in $em->flush(); or could look like:

try {
    $parentEntity = $em->gerReference('ParentEntity', $id);
catch (SomeORMNamespace\ConstraintException $e) {
    echo $e->getMessage(); //outs: You cannot remove this entity. There is another one related.

I think this functionality could be implemented through metadata on any sides of the relationship or on the @Entity mapping.

@Entity(children={'Child'}) //this is not a good attr name =)
class Parent {}

I know that is more app business logic related, but is it possible to implement this feature in next releases?

Comment by Benjamin Eberlei [ 15/Feb/13 ]

This is responsiblity of the the application developer.

You can implement this generically using parts of the API shown below:

$metadata = $entityManager->getClassMetadata("ClassName");

foreach ($metadata->associationMappings as $assoc) {
   if ($assoc['type'] & ClassMetadata::TO_ONE) {
Generated at Tue Dec 01 18:52:19 EST 2015 using JIRA 6.4.10#64025-sha1:5b8b74079161cd76a20ab66dda52747ee6701bd6.