Skip to content

Loading…

DDC-2297: Add ConstraintException for parent row #2998

Closed
doctrinebot opened this Issue · 2 comments

2 participants

@doctrinebot

Jira issue originally created by user yourwebmaker:

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');
}

$em->remove($parent);
$em->flush();

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

try {
    $parentEntity = $em->gerReference('ParentEntity', $id);
    $em->remove($parentEntity);
    $em->flush();
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?

@doctrinebot

Comment created by @beberlei:

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) {

   }
}
@doctrinebot

Issue was closed with resolution "Won't Fix"

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.