123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
-
-
- namespace Doctrine\ORM\Tools;
-
- use Doctrine\ORM\EntityManager;
- use Doctrine\ORM\Mapping\ClassMetadataInfo;
-
-
- class SchemaValidator
- {
-
-
- private $em;
-
-
-
- public function __construct(EntityManager $em)
- {
- $this->em = $em;
- }
-
-
-
- public function validateMapping()
- {
- $errors = array();
- $cmf = $this->em->getMetadataFactory();
- $classes = $cmf->getAllMetadata();
-
- foreach ($classes AS $class) {
- $ce = array();
-
- foreach ($class->associationMappings AS $fieldName => $assoc) {
- if (!$cmf->hasMetadataFor($assoc['targetEntity'])) {
- $ce[] = "The target entity '" . $assoc['targetEntity'] . "' specified on " . $class->name . '#' . $fieldName . ' is unknown.';
- }
-
- if ($assoc['mappedBy'] && $assoc['inversedBy']) {
- $ce[] = "The association " . $class . "#" . $fieldName . " cannot be defined as both inverse and owning.";
- }
-
- $targetMetadata = $cmf->getMetadataFor($assoc['targetEntity']);
-
-
- if ($assoc['mappedBy']) {
- if ($targetMetadata->hasField($assoc['mappedBy'])) {
- $ce[] = "The association " . $class->name . "#" . $fieldName . " refers to the owning side ".
- "field " . $assoc['targetEntity'] . "#" . $assoc['mappedBy'] . " which is not defined as association.";
- }
- if (!$targetMetadata->hasAssociation($assoc['mappedBy'])) {
- $ce[] = "The association " . $class->name . "#" . $fieldName . " refers to the owning side ".
- "field " . $assoc['targetEntity'] . "#" . $assoc['mappedBy'] . " which does not exist.";
- } else if ($targetMetadata->associationMappings[$assoc['mappedBy']]['inversedBy'] == null) {
- $ce[] = "The field " . $class->name . "#" . $fieldName . " is on the inverse side of a ".
- "bi-directional relationship, but the specified mappedBy association on the target-entity ".
- $assoc['targetEntity'] . "#" . $assoc['mappedBy'] . " does not contain the required ".
- "'inversedBy' attribute.";
- } else if ($targetMetadata->associationMappings[$assoc['mappedBy']]['inversedBy'] != $fieldName) {
- $ce[] = "The mappings " . $class->name . "#" . $fieldName . " and " .
- $assoc['targetEntity'] . "#" . $assoc['mappedBy'] . " are ".
- "incosistent with each other.";
- }
- }
-
- if ($assoc['inversedBy']) {
- if ($targetMetadata->hasField($assoc['inversedBy'])) {
- $ce[] = "The association " . $class->name . "#" . $fieldName . " refers to the inverse side ".
- "field " . $assoc['targetEntity'] . "#" . $assoc['inversedBy'] . " which is not defined as association.";
- }
- if (!$targetMetadata->hasAssociation($assoc['inversedBy'])) {
- $ce[] = "The association " . $class->name . "#" . $fieldName . " refers to the inverse side ".
- "field " . $assoc['targetEntity'] . "#" . $assoc['inversedBy'] . " which does not exist.";
- } else if ($targetMetadata->associationMappings[$assoc['inversedBy']]['mappedBy'] == null) {
- $ce[] = "The field " . $class->name . "#" . $fieldName . " is on the owning side of a ".
- "bi-directional relationship, but the specified mappedBy association on the target-entity ".
- $assoc['targetEntity'] . "#" . $assoc['mappedBy'] . " does not contain the required ".
- "'inversedBy' attribute.";
- } else if ($targetMetadata->associationMappings[$assoc['inversedBy']]['mappedBy'] != $fieldName) {
- $ce[] = "The mappings " . $class->name . "#" . $fieldName . " and " .
- $assoc['targetEntity'] . "#" . $assoc['inversedBy'] . " are ".
- "incosistent with each other.";
- }
- }
-
- if ($assoc['isOwningSide']) {
- if ($assoc['type'] == ClassMetadataInfo::MANY_TO_MANY) {
- foreach ($assoc['joinTable']['joinColumns'] AS $joinColumn) {
- if (!isset($class->fieldNames[$joinColumn['referencedColumnName']])) {
- $ce[] = "The referenced column name '" . $joinColumn['referencedColumnName'] . "' does not " .
- "have a corresponding field with this column name on the class '" . $class->name . "'.";
- break;
- }
-
- $fieldName = $class->fieldNames[$joinColumn['referencedColumnName']];
- if (!in_array($fieldName, $class->identifier)) {
- $ce[] = "The referenced column name '" . $joinColumn['referencedColumnName'] . "' " .
- "has to be a primary key column.";
- }
- }
- foreach ($assoc['joinTable']['inverseJoinColumns'] AS $inverseJoinColumn) {
- $targetClass = $cmf->getMetadataFor($assoc['targetEntity']);
- if (!isset($targetClass->fieldNames[$inverseJoinColumn['referencedColumnName']])) {
- $ce[] = "The inverse referenced column name '" . $inverseJoinColumn['referencedColumnName'] . "' does not " .
- "have a corresponding field with this column name on the class '" . $targetClass->name . "'.";
- break;
- }
-
- $fieldName = $targetClass->fieldNames[$inverseJoinColumn['referencedColumnName']];
- if (!in_array($fieldName, $targetClass->identifier)) {
- $ce[] = "The referenced column name '" . $inverseJoinColumn['referencedColumnName'] . "' " .
- "has to be a primary key column.";
- }
- }
- } else if ($assoc['type'] & ClassMetadataInfo::TO_ONE) {
- foreach ($assoc['joinColumns'] AS $joinColumn) {
- $targetClass = $cmf->getMetadataFor($assoc['targetEntity']);
- if (!isset($targetClass->fieldNames[$joinColumn['referencedColumnName']])) {
- $ce[] = "The referenced column name '" . $joinColumn['referencedColumnName'] . "' does not " .
- "have a corresponding field with this column name on the class '" . $targetClass->name . "'.";
- break;
- }
-
- $fieldName = $targetClass->fieldNames[$joinColumn['referencedColumnName']];
- if (!in_array($fieldName, $targetClass->identifier)) {
- $ce[] = "The referenced column name '" . $joinColumn['referencedColumnName'] . "' " .
- "has to be a primary key column.";
- }
- }
- }
- }
-
- if (isset($assoc['orderBy']) && $assoc['orderBy'] !== null) {
- $targetClass = $cmf->getMetadataFor($assoc['targetEntity']);
- foreach ($assoc['orderBy'] AS $orderField => $orientation) {
- if (!$targetClass->hasField($orderField)) {
- $ce[] = "The association " . $class->name."#".$fieldName." is ordered by a foreign field " .
- $orderField . " that is not a field on the target entity " . $targetClass->name;
- }
- }
- }
- }
-
- foreach ($class->reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $publicAttr) {
- if ($publicAttr->isStatic()) {
- continue;
- }
- $ce[] = "Field '".$publicAttr->getName()."' in class '".$class->name."' must be private ".
- "or protected. Public fields may break lazy-loading.";
- }
-
- foreach ($class->subClasses AS $subClass) {
- if (!in_array($class->name, class_parents($subClass))) {
- $ce[] = "According to the discriminator map class '" . $subClass . "' has to be a child ".
- "of '" . $class->name . "' but these entities are not related through inheritance.";
- }
- }
-
- if ($ce) {
- $errors[$class->name] = $ce;
- }
- }
-
- return $errors;
- }
-
-
-
- public function schemaInSyncWithMetadata()
- {
- $schemaTool = new SchemaTool($this->em);
-
- $allMetadata = $this->em->getMetadataFactory()->getAllMetadata();
- return (count($schemaTool->getUpdateSchemaSql($allMetadata, true)) == 0);
- }
- }
|