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

Open in your IDE?
  1. <?php
  2. /*
  3.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  *
  15.  * This software consists of voluntary contributions made by many individuals
  16.  * and is licensed under the MIT license. For more information, see
  17.  * <http://www.doctrine-project.org>.
  18.  */
  19. namespace Doctrine\ORM\Tools;
  20. use Doctrine\ORM\EntityManagerInterface;
  21. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  22. use Doctrine\DBAL\Types\Type;
  23. use function count;
  24. /**
  25.  * Performs strict validation of the mapping schema
  26.  *
  27.  * @license     http://www.opensource.org/licenses/mit-license.php MIT
  28.  * @link        www.doctrine-project.com
  29.  * @since       1.0
  30.  * @author      Benjamin Eberlei <kontakt@beberlei.de>
  31.  * @author      Guilherme Blanco <guilhermeblanco@hotmail.com>
  32.  * @author      Jonathan Wage <jonwage@gmail.com>
  33.  * @author      Roman Borschel <roman@code-factory.org>
  34.  */
  35. class SchemaValidator
  36. {
  37.     /**
  38.      * @var EntityManagerInterface
  39.      */
  40.     private $em;
  41.     /**
  42.      * @param EntityManagerInterface $em
  43.      */
  44.     public function __construct(EntityManagerInterface $em)
  45.     {
  46.         $this->em $em;
  47.     }
  48.     /**
  49.      * Checks the internal consistency of all mapping files.
  50.      *
  51.      * There are several checks that can't be done at runtime or are too expensive, which can be verified
  52.      * with this command. For example:
  53.      *
  54.      * 1. Check if a relation with "mappedBy" is actually connected to that specified field.
  55.      * 2. Check if "mappedBy" and "inversedBy" are consistent to each other.
  56.      * 3. Check if "referencedColumnName" attributes are really pointing to primary key columns.
  57.      *
  58.      * @return array
  59.      */
  60.     public function validateMapping()
  61.     {
  62.         $errors = [];
  63.         $cmf $this->em->getMetadataFactory();
  64.         $classes $cmf->getAllMetadata();
  65.         foreach ($classes as $class) {
  66.             if ($ce $this->validateClass($class)) {
  67.                 $errors[$class->name] = $ce;
  68.             }
  69.         }
  70.         return $errors;
  71.     }
  72.     /**
  73.      * Validates a single class of the current.
  74.      *
  75.      * @param ClassMetadataInfo $class
  76.      *
  77.      * @return string[]
  78.      *
  79.      * @psalm-return list<string>
  80.      */
  81.     public function validateClass(ClassMetadataInfo $class)
  82.     {
  83.         $ce = [];
  84.         $cmf $this->em->getMetadataFactory();
  85.         foreach ($class->fieldMappings as $fieldName => $mapping) {
  86.             if (!Type::hasType($mapping['type'])) {
  87.                 $ce[] = "The field '" $class->name "#" $fieldName."' uses a non-existent type '" $mapping['type'] . "'.";
  88.             }
  89.         }
  90.         if ($class->isEmbeddedClass && count($class->associationMappings) > 0) {
  91.             $ce[] = "Embeddable '" $class->name "' does not support associations";
  92.             return $ce;
  93.         }
  94.         foreach ($class->associationMappings as $fieldName => $assoc) {
  95.             if (!class_exists($assoc['targetEntity']) || $cmf->isTransient($assoc['targetEntity'])) {
  96.                 $ce[] = "The target entity '" $assoc['targetEntity'] . "' specified on " $class->name '#' $fieldName ' is unknown or not an entity.';
  97.                 return $ce;
  98.             }
  99.             if ($assoc['mappedBy'] && $assoc['inversedBy']) {
  100.                 $ce[] = "The association " $class "#" $fieldName " cannot be defined as both inverse and owning.";
  101.             }
  102.             $targetMetadata $cmf->getMetadataFor($assoc['targetEntity']);
  103.             if (isset($assoc['id']) && $targetMetadata->containsForeignIdentifier) {
  104.                 $ce[] = "Cannot map association '" $class->name"#"$fieldName ." as identifier, because " .
  105.                         "the target entity '"$targetMetadata->name "' also maps an association as identifier.";
  106.             }
  107.             if ($assoc['mappedBy']) {
  108.                 if ($targetMetadata->hasField($assoc['mappedBy'])) {
  109.                     $ce[] = "The association " $class->name "#" $fieldName " refers to the owning side ".
  110.                             "field " $assoc['targetEntity'] . "#" $assoc['mappedBy'] . " which is not defined as association, but as field.";
  111.                 }
  112.                 if (!$targetMetadata->hasAssociation($assoc['mappedBy'])) {
  113.                     $ce[] = "The association " $class->name "#" $fieldName " refers to the owning side ".
  114.                             "field " $assoc['targetEntity'] . "#" $assoc['mappedBy'] . " which does not exist.";
  115.                 } elseif ($targetMetadata->associationMappings[$assoc['mappedBy']]['inversedBy'] == null) {
  116.                     $ce[] = "The field " $class->name "#" $fieldName " is on the inverse side of a ".
  117.                             "bi-directional relationship, but the specified mappedBy association on the target-entity ".
  118.                             $assoc['targetEntity'] . "#" $assoc['mappedBy'] . " does not contain the required ".
  119.                             "'inversedBy=\"" $fieldName "\"' attribute.";
  120.                 } elseif ($targetMetadata->associationMappings[$assoc['mappedBy']]['inversedBy'] != $fieldName) {
  121.                     $ce[] = "The mappings " $class->name "#" $fieldName " and " .
  122.                             $assoc['targetEntity'] . "#" $assoc['mappedBy'] . " are ".
  123.                             "inconsistent with each other.";
  124.                 }
  125.             }
  126.             if ($assoc['inversedBy']) {
  127.                 if ($targetMetadata->hasField($assoc['inversedBy'])) {
  128.                     $ce[] = "The association " $class->name "#" $fieldName " refers to the inverse side ".
  129.                             "field " $assoc['targetEntity'] . "#" $assoc['inversedBy'] . " which is not defined as association.";
  130.                 }
  131.                 if (!$targetMetadata->hasAssociation($assoc['inversedBy'])) {
  132.                     $ce[] = "The association " $class->name "#" $fieldName " refers to the inverse side ".
  133.                             "field " $assoc['targetEntity'] . "#" $assoc['inversedBy'] . " which does not exist.";
  134.                 } elseif ($targetMetadata->associationMappings[$assoc['inversedBy']]['mappedBy'] == null) {
  135.                     $ce[] = "The field " $class->name "#" $fieldName " is on the owning side of a ".
  136.                             "bi-directional relationship, but the specified mappedBy association on the target-entity ".
  137.                             $assoc['targetEntity'] . "#" $assoc['mappedBy'] . " does not contain the required ".
  138.                             "'inversedBy' attribute.";
  139.                 } elseif ($targetMetadata->associationMappings[$assoc['inversedBy']]['mappedBy'] != $fieldName) {
  140.                     $ce[] = "The mappings " $class->name "#" $fieldName " and " .
  141.                             $assoc['targetEntity'] . "#" $assoc['inversedBy'] . " are ".
  142.                             "inconsistent with each other.";
  143.                 }
  144.                 // Verify inverse side/owning side match each other
  145.                 if (array_key_exists($assoc['inversedBy'], $targetMetadata->associationMappings)) {
  146.                     $targetAssoc $targetMetadata->associationMappings[$assoc['inversedBy']];
  147.                     if ($assoc['type'] == ClassMetadataInfo::ONE_TO_ONE && $targetAssoc['type'] !== ClassMetadataInfo::ONE_TO_ONE) {
  148.                         $ce[] = "If association " $class->name "#" $fieldName " is one-to-one, then the inversed " .
  149.                                 "side " $targetMetadata->name "#" $assoc['inversedBy'] . " has to be one-to-one as well.";
  150.                     } elseif ($assoc['type'] == ClassMetadataInfo::MANY_TO_ONE && $targetAssoc['type'] !== ClassMetadataInfo::ONE_TO_MANY) {
  151.                         $ce[] = "If association " $class->name "#" $fieldName " is many-to-one, then the inversed " .
  152.                                 "side " $targetMetadata->name "#" $assoc['inversedBy'] . " has to be one-to-many.";
  153.                     } elseif ($assoc['type'] == ClassMetadataInfo::MANY_TO_MANY && $targetAssoc['type'] !== ClassMetadataInfo::MANY_TO_MANY) {
  154.                         $ce[] = "If association " $class->name "#" $fieldName " is many-to-many, then the inversed " .
  155.                                 "side " $targetMetadata->name "#" $assoc['inversedBy'] . " has to be many-to-many as well.";
  156.                     }
  157.                 }
  158.             }
  159.             if ($assoc['isOwningSide']) {
  160.                 if ($assoc['type'] == ClassMetadataInfo::MANY_TO_MANY) {
  161.                     $identifierColumns $class->getIdentifierColumnNames();
  162.                     foreach ($assoc['joinTable']['joinColumns'] as $joinColumn) {
  163.                         if (!in_array($joinColumn['referencedColumnName'], $identifierColumns)) {
  164.                             $ce[] = "The referenced column name '" $joinColumn['referencedColumnName'] . "' " .
  165.                                 "has to be a primary key column on the target entity class '".$class->name."'.";
  166.                             break;
  167.                         }
  168.                     }
  169.                     $identifierColumns $targetMetadata->getIdentifierColumnNames();
  170.                     foreach ($assoc['joinTable']['inverseJoinColumns'] as $inverseJoinColumn) {
  171.                         if (! in_array($inverseJoinColumn['referencedColumnName'], $identifierColumns)) {
  172.                             $ce[] = "The referenced column name '" $inverseJoinColumn['referencedColumnName'] . "' " .
  173.                                 "has to be a primary key column on the target entity class '" .$targetMetadata->name "'.";
  174.                             break;
  175.                         }
  176.                     }
  177.                     if (count($targetMetadata->getIdentifierColumnNames()) != count($assoc['joinTable']['inverseJoinColumns'])) {
  178.                         $ce[] = "The inverse join columns of the many-to-many table '" $assoc['joinTable']['name'] . "' " .
  179.                                 "have to contain to ALL identifier columns of the target entity '"$targetMetadata->name "', " .
  180.                                 "however '" implode(", "array_diff($targetMetadata->getIdentifierColumnNames(), array_values($assoc['relationToTargetKeyColumns']))) .
  181.                                 "' are missing.";
  182.                     }
  183.                     if (count($class->getIdentifierColumnNames()) != count($assoc['joinTable']['joinColumns'])) {
  184.                         $ce[] = "The join columns of the many-to-many table '" $assoc['joinTable']['name'] . "' " .
  185.                                 "have to contain to ALL identifier columns of the source entity '"$class->name "', " .
  186.                                 "however '" implode(", "array_diff($class->getIdentifierColumnNames(), array_values($assoc['relationToSourceKeyColumns']))) .
  187.                                 "' are missing.";
  188.                     }
  189.                 } elseif ($assoc['type'] & ClassMetadataInfo::TO_ONE) {
  190.                     $identifierColumns $targetMetadata->getIdentifierColumnNames();
  191.                     foreach ($assoc['joinColumns'] as $joinColumn) {
  192.                         if (!in_array($joinColumn['referencedColumnName'], $identifierColumns)) {
  193.                             $ce[] = "The referenced column name '" $joinColumn['referencedColumnName'] . "' " .
  194.                                     "has to be a primary key column on the target entity class '".$targetMetadata->name."'.";
  195.                         }
  196.                     }
  197.                     if (count($identifierColumns) != count($assoc['joinColumns'])) {
  198.                         $ids = [];
  199.                         foreach ($assoc['joinColumns'] as $joinColumn) {
  200.                             $ids[] = $joinColumn['name'];
  201.                         }
  202.                         $ce[] = "The join columns of the association '" $assoc['fieldName'] . "' " .
  203.                                 "have to match to ALL identifier columns of the target entity '"$targetMetadata->name "', " .
  204.                                 "however '" implode(", "array_diff($targetMetadata->getIdentifierColumnNames(), $ids)) .
  205.                                 "' are missing.";
  206.                     }
  207.                 }
  208.             }
  209.             if (isset($assoc['orderBy']) && $assoc['orderBy'] !== null) {
  210.                 foreach ($assoc['orderBy'] as $orderField => $orientation) {
  211.                     if (!$targetMetadata->hasField($orderField) && !$targetMetadata->hasAssociation($orderField)) {
  212.                         $ce[] = "The association " $class->name."#".$fieldName." is ordered by a foreign field " .
  213.                                 $orderField " that is not a field on the target entity " $targetMetadata->name ".";
  214.                         continue;
  215.                     }
  216.                     if ($targetMetadata->isCollectionValuedAssociation($orderField)) {
  217.                         $ce[] = "The association " $class->name."#".$fieldName." is ordered by a field " .
  218.                                 $orderField " on " $targetMetadata->name " that is a collection-valued association.";
  219.                         continue;
  220.                     }
  221.                     if ($targetMetadata->isAssociationInverseSide($orderField)) {
  222.                         $ce[] = "The association " $class->name."#".$fieldName." is ordered by a field " .
  223.                                 $orderField " on " $targetMetadata->name " that is the inverse side of an association.";
  224.                         continue;
  225.                     }
  226.                 }
  227.             }
  228.         }
  229.         foreach ($class->subClasses as $subClass) {
  230.             if (!in_array($class->nameclass_parents($subClass))) {
  231.                 $ce[] = "According to the discriminator map class '" $subClass "' has to be a child ".
  232.                         "of '" $class->name "' but these entities are not related through inheritance.";
  233.             }
  234.         }
  235.         return $ce;
  236.     }
  237.     /**
  238.      * Checks if the Database Schema is in sync with the current metadata state.
  239.      *
  240.      * @return bool
  241.      */
  242.     public function schemaInSyncWithMetadata()
  243.     {
  244.         $schemaTool = new SchemaTool($this->em);
  245.         $allMetadata $this->em->getMetadataFactory()->getAllMetadata();
  246.         return count($schemaTool->getUpdateSchemaSql($allMetadatatrue)) == 0;
  247.     }
  248. }