vendor/doctrine/orm/lib/Doctrine/ORM/Tools/SchemaValidator.php line 70

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\ORM\Tools;
  4. use Doctrine\DBAL\Types\Type;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  7. use function array_diff;
  8. use function array_key_exists;
  9. use function array_search;
  10. use function array_values;
  11. use function class_exists;
  12. use function class_parents;
  13. use function count;
  14. use function implode;
  15. use function in_array;
  16. /**
  17.  * Performs strict validation of the mapping schema
  18.  *
  19.  * @link        www.doctrine-project.com
  20.  */
  21. class SchemaValidator
  22. {
  23.     /** @var EntityManagerInterface */
  24.     private $em;
  25.     public function __construct(EntityManagerInterface $em)
  26.     {
  27.         $this->em $em;
  28.     }
  29.     /**
  30.      * Checks the internal consistency of all mapping files.
  31.      *
  32.      * There are several checks that can't be done at runtime or are too expensive, which can be verified
  33.      * with this command. For example:
  34.      *
  35.      * 1. Check if a relation with "mappedBy" is actually connected to that specified field.
  36.      * 2. Check if "mappedBy" and "inversedBy" are consistent to each other.
  37.      * 3. Check if "referencedColumnName" attributes are really pointing to primary key columns.
  38.      *
  39.      * @psalm-return array<string, list<string>>
  40.      */
  41.     public function validateMapping()
  42.     {
  43.         $errors  = [];
  44.         $cmf     $this->em->getMetadataFactory();
  45.         $classes $cmf->getAllMetadata();
  46.         foreach ($classes as $class) {
  47.             $ce $this->validateClass($class);
  48.             if ($ce) {
  49.                 $errors[$class->name] = $ce;
  50.             }
  51.         }
  52.         return $errors;
  53.     }
  54.     /**
  55.      * Validates a single class of the current.
  56.      *
  57.      * @return string[]
  58.      * @psalm-return list<string>
  59.      */
  60.     public function validateClass(ClassMetadataInfo $class)
  61.     {
  62.         $ce  = [];
  63.         $cmf $this->em->getMetadataFactory();
  64.         foreach ($class->fieldMappings as $fieldName => $mapping) {
  65.             if (! Type::hasType($mapping['type'])) {
  66.                 $ce[] = "The field '" $class->name '#' $fieldName "' uses a non-existent type '" $mapping['type'] . "'.";
  67.             }
  68.         }
  69.         if ($class->isEmbeddedClass && count($class->associationMappings) > 0) {
  70.             $ce[] = "Embeddable '" $class->name "' does not support associations";
  71.             return $ce;
  72.         }
  73.         foreach ($class->associationMappings as $fieldName => $assoc) {
  74.             if (! class_exists($assoc['targetEntity']) || $cmf->isTransient($assoc['targetEntity'])) {
  75.                 $ce[] = "The target entity '" $assoc['targetEntity'] . "' specified on " $class->name '#' $fieldName ' is unknown or not an entity.';
  76.                 return $ce;
  77.             }
  78.             if ($assoc['mappedBy'] && $assoc['inversedBy']) {
  79.                 $ce[] = 'The association ' $class '#' $fieldName ' cannot be defined as both inverse and owning.';
  80.             }
  81.             $targetMetadata $cmf->getMetadataFor($assoc['targetEntity']);
  82.             if (isset($assoc['id']) && $targetMetadata->containsForeignIdentifier) {
  83.                 $ce[] = "Cannot map association '" $class->name '#' $fieldName ' as identifier, because ' .
  84.                         "the target entity '" $targetMetadata->name "' also maps an association as identifier.";
  85.             }
  86.             if ($assoc['mappedBy']) {
  87.                 if ($targetMetadata->hasField($assoc['mappedBy'])) {
  88.                     $ce[] = 'The association ' $class->name '#' $fieldName ' refers to the owning side ' .
  89.                             'field ' $assoc['targetEntity'] . '#' $assoc['mappedBy'] . ' which is not defined as association, but as field.';
  90.                 }
  91.                 if (! $targetMetadata->hasAssociation($assoc['mappedBy'])) {
  92.                     $ce[] = 'The association ' $class->name '#' $fieldName ' refers to the owning side ' .
  93.                             'field ' $assoc['targetEntity'] . '#' $assoc['mappedBy'] . ' which does not exist.';
  94.                 } elseif ($targetMetadata->associationMappings[$assoc['mappedBy']]['inversedBy'] === null) {
  95.                     $ce[] = 'The field ' $class->name '#' $fieldName ' is on the inverse side of a ' .
  96.                             'bi-directional relationship, but the specified mappedBy association on the target-entity ' .
  97.                             $assoc['targetEntity'] . '#' $assoc['mappedBy'] . ' does not contain the required ' .
  98.                             "'inversedBy=\"" $fieldName "\"' attribute.";
  99.                 } elseif ($targetMetadata->associationMappings[$assoc['mappedBy']]['inversedBy'] !== $fieldName) {
  100.                     $ce[] = 'The mappings ' $class->name '#' $fieldName ' and ' .
  101.                             $assoc['targetEntity'] . '#' $assoc['mappedBy'] . ' are ' .
  102.                             'inconsistent with each other.';
  103.                 }
  104.             }
  105.             if ($assoc['inversedBy']) {
  106.                 if ($targetMetadata->hasField($assoc['inversedBy'])) {
  107.                     $ce[] = 'The association ' $class->name '#' $fieldName ' refers to the inverse side ' .
  108.                             'field ' $assoc['targetEntity'] . '#' $assoc['inversedBy'] . ' which is not defined as association.';
  109.                 }
  110.                 if (! $targetMetadata->hasAssociation($assoc['inversedBy'])) {
  111.                     $ce[] = 'The association ' $class->name '#' $fieldName ' refers to the inverse side ' .
  112.                             'field ' $assoc['targetEntity'] . '#' $assoc['inversedBy'] . ' which does not exist.';
  113.                 } elseif ($targetMetadata->associationMappings[$assoc['inversedBy']]['mappedBy'] === null) {
  114.                     $ce[] = 'The field ' $class->name '#' $fieldName ' is on the owning side of a ' .
  115.                             'bi-directional relationship, but the specified mappedBy association on the target-entity ' .
  116.                             $assoc['targetEntity'] . '#' $assoc['mappedBy'] . ' does not contain the required ' .
  117.                             "'inversedBy' attribute.";
  118.                 } elseif ($targetMetadata->associationMappings[$assoc['inversedBy']]['mappedBy'] !== $fieldName) {
  119.                     $ce[] = 'The mappings ' $class->name '#' $fieldName ' and ' .
  120.                             $assoc['targetEntity'] . '#' $assoc['inversedBy'] . ' are ' .
  121.                             'inconsistent with each other.';
  122.                 }
  123.                 // Verify inverse side/owning side match each other
  124.                 if (array_key_exists($assoc['inversedBy'], $targetMetadata->associationMappings)) {
  125.                     $targetAssoc $targetMetadata->associationMappings[$assoc['inversedBy']];
  126.                     if ($assoc['type'] === ClassMetadataInfo::ONE_TO_ONE && $targetAssoc['type'] !== ClassMetadataInfo::ONE_TO_ONE) {
  127.                         $ce[] = 'If association ' $class->name '#' $fieldName ' is one-to-one, then the inversed ' .
  128.                                 'side ' $targetMetadata->name '#' $assoc['inversedBy'] . ' has to be one-to-one as well.';
  129.                     } elseif ($assoc['type'] === ClassMetadataInfo::MANY_TO_ONE && $targetAssoc['type'] !== ClassMetadataInfo::ONE_TO_MANY) {
  130.                         $ce[] = 'If association ' $class->name '#' $fieldName ' is many-to-one, then the inversed ' .
  131.                                 'side ' $targetMetadata->name '#' $assoc['inversedBy'] . ' has to be one-to-many.';
  132.                     } elseif ($assoc['type'] === ClassMetadataInfo::MANY_TO_MANY && $targetAssoc['type'] !== ClassMetadataInfo::MANY_TO_MANY) {
  133.                         $ce[] = 'If association ' $class->name '#' $fieldName ' is many-to-many, then the inversed ' .
  134.                                 'side ' $targetMetadata->name '#' $assoc['inversedBy'] . ' has to be many-to-many as well.';
  135.                     }
  136.                 }
  137.             }
  138.             if ($assoc['isOwningSide']) {
  139.                 if ($assoc['type'] === ClassMetadataInfo::MANY_TO_MANY) {
  140.                     $identifierColumns $class->getIdentifierColumnNames();
  141.                     foreach ($assoc['joinTable']['joinColumns'] as $joinColumn) {
  142.                         if (! in_array($joinColumn['referencedColumnName'], $identifierColumnstrue)) {
  143.                             $ce[] = "The referenced column name '" $joinColumn['referencedColumnName'] . "' " .
  144.                                 "has to be a primary key column on the target entity class '" $class->name "'.";
  145.                             break;
  146.                         }
  147.                     }
  148.                     $identifierColumns $targetMetadata->getIdentifierColumnNames();
  149.                     foreach ($assoc['joinTable']['inverseJoinColumns'] as $inverseJoinColumn) {
  150.                         if (! in_array($inverseJoinColumn['referencedColumnName'], $identifierColumnstrue)) {
  151.                             $ce[] = "The referenced column name '" $inverseJoinColumn['referencedColumnName'] . "' " .
  152.                                 "has to be a primary key column on the target entity class '" $targetMetadata->name "'.";
  153.                             break;
  154.                         }
  155.                     }
  156.                     if (count($targetMetadata->getIdentifierColumnNames()) !== count($assoc['joinTable']['inverseJoinColumns'])) {
  157.                         $ce[] = "The inverse join columns of the many-to-many table '" $assoc['joinTable']['name'] . "' " .
  158.                                 "have to contain to ALL identifier columns of the target entity '" $targetMetadata->name "', " .
  159.                                 "however '" implode(', 'array_diff($targetMetadata->getIdentifierColumnNames(), array_values($assoc['relationToTargetKeyColumns']))) .
  160.                                 "' are missing.";
  161.                     }
  162.                     if (count($class->getIdentifierColumnNames()) !== count($assoc['joinTable']['joinColumns'])) {
  163.                         $ce[] = "The join columns of the many-to-many table '" $assoc['joinTable']['name'] . "' " .
  164.                                 "have to contain to ALL identifier columns of the source entity '" $class->name "', " .
  165.                                 "however '" implode(', 'array_diff($class->getIdentifierColumnNames(), array_values($assoc['relationToSourceKeyColumns']))) .
  166.                                 "' are missing.";
  167.                     }
  168.                 } elseif ($assoc['type'] & ClassMetadataInfo::TO_ONE) {
  169.                     $identifierColumns $targetMetadata->getIdentifierColumnNames();
  170.                     foreach ($assoc['joinColumns'] as $joinColumn) {
  171.                         if (! in_array($joinColumn['referencedColumnName'], $identifierColumnstrue)) {
  172.                             $ce[] = "The referenced column name '" $joinColumn['referencedColumnName'] . "' " .
  173.                                     "has to be a primary key column on the target entity class '" $targetMetadata->name "'.";
  174.                         }
  175.                     }
  176.                     if (count($identifierColumns) !== count($assoc['joinColumns'])) {
  177.                         $ids = [];
  178.                         foreach ($assoc['joinColumns'] as $joinColumn) {
  179.                             $ids[] = $joinColumn['name'];
  180.                         }
  181.                         $ce[] = "The join columns of the association '" $assoc['fieldName'] . "' " .
  182.                                 "have to match to ALL identifier columns of the target entity '" $targetMetadata->name "', " .
  183.                                 "however '" implode(', 'array_diff($targetMetadata->getIdentifierColumnNames(), $ids)) .
  184.                                 "' are missing.";
  185.                     }
  186.                 }
  187.             }
  188.             if (isset($assoc['orderBy']) && $assoc['orderBy'] !== null) {
  189.                 foreach ($assoc['orderBy'] as $orderField => $orientation) {
  190.                     if (! $targetMetadata->hasField($orderField) && ! $targetMetadata->hasAssociation($orderField)) {
  191.                         $ce[] = 'The association ' $class->name '#' $fieldName ' is ordered by a foreign field ' .
  192.                                 $orderField ' that is not a field on the target entity ' $targetMetadata->name '.';
  193.                         continue;
  194.                     }
  195.                     if ($targetMetadata->isCollectionValuedAssociation($orderField)) {
  196.                         $ce[] = 'The association ' $class->name '#' $fieldName ' is ordered by a field ' .
  197.                                 $orderField ' on ' $targetMetadata->name ' that is a collection-valued association.';
  198.                         continue;
  199.                     }
  200.                     if ($targetMetadata->isAssociationInverseSide($orderField)) {
  201.                         $ce[] = 'The association ' $class->name '#' $fieldName ' is ordered by a field ' .
  202.                                 $orderField ' on ' $targetMetadata->name ' that is the inverse side of an association.';
  203.                         continue;
  204.                     }
  205.                 }
  206.             }
  207.         }
  208.         if (! $class->isInheritanceTypeNone() && ! $class->isRootEntity() && ! $class->reflClass->isAbstract() && ! $class->isMappedSuperclass && array_search($class->name$class->discriminatorMaptrue) === false) {
  209.             $ce[] = "Entity class '" $class->name "' is part of inheritance hierarchy, but is " .
  210.                 "not mapped in the root entity '" $class->rootEntityName "' discriminator map. " .
  211.                 'All subclasses must be listed in the discriminator map.';
  212.         }
  213.         foreach ($class->subClasses as $subClass) {
  214.             if (! in_array($class->nameclass_parents($subClass), true)) {
  215.                 $ce[] = "According to the discriminator map class '" $subClass "' has to be a child " .
  216.                         "of '" $class->name "' but these entities are not related through inheritance.";
  217.             }
  218.         }
  219.         return $ce;
  220.     }
  221.     /**
  222.      * Checks if the Database Schema is in sync with the current metadata state.
  223.      *
  224.      * @return bool
  225.      */
  226.     public function schemaInSyncWithMetadata()
  227.     {
  228.         $schemaTool = new SchemaTool($this->em);
  229.         $allMetadata $this->em->getMetadataFactory()->getAllMetadata();
  230.         return count($schemaTool->getUpdateSchemaSql($allMetadatatrue)) === 0;
  231.     }
  232. }