protected function checkRelations(LifecycleEventArgs $args) { $entity = $args->getEntity(); $entityManager = $args->getEntityManager(); $entityName = (new \ReflectionClass($entity))->getShortName(); if (in_array($entityManager->getClassMetadata(get_class($entity))->name, $this->excludedEntities)) { return; } $mappings = $entityManager->getClassMetadata(get_class($entity))->getAssociationMappings(); foreach ($mappings as $mapping) { if ($mapping['type'] === ClassMetadataInfo::ONE_TO_MANY || $mapping['type'] === ClassMetadataInfo::MANY_TO_MANY) { $targetEntityMeta = $entityManager->getClassMetadata($mapping['targetEntity']); if ($targetEntityMeta->reflClass->getParentClass()) { if (in_array($targetEntityMeta->reflClass->getParentClass()->name, $this->ignoredEntities)) { continue; } } $accessor = PropertyAccess::createPropertyAccessor(); $value = $accessor->getValue($entity, $mapping['fieldName']); if (count($value) === 0) { continue; } $exception = new HasRelationException(); $exception->setEntityName($entityName); $exception->setEntity($entity); $exception->setMapping($mapping); throw $exception; } } }
/** * @return bool * @throws HasRelationException */ private function checkUse() { if (!count($this->checkUse)) { return true; } foreach ($this->checkUse as $usage) { $findRelations = $this->findRelations($usage); if (count($findRelations) > 0) { $relationStrings = []; foreach ($findRelations as $relation) { $relationRefl = new \ReflectionClass($relation); $relationStrings[] = (string) $relation . '[' . $relationRefl->getShortName() . '-' . $usage['field'] . '#' . $relation->getId() . ']'; } $hasRelationException = new HasRelationException(); $hasRelationException->setErrorMessage($this->translator->trans('deletion.remove_components_first', ['%field%' => "\n - " . implode("\n - ", $relationStrings)])); throw $hasRelationException; } } return true; }