2010-05-12 01:08:36 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.doctrine-project.org>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Doctrine\ORM\Tools;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManager;
|
2010-08-09 15:13:21 +04:00
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
2011-12-19 13:10:56 +04:00
|
|
|
use Doctrine\DBAL\Types\Type;
|
2010-05-12 01:08:36 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs strict validation of the mapping schema
|
|
|
|
*
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @link www.doctrine-project.com
|
|
|
|
* @since 1.0
|
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @author Jonathan Wage <jonwage@gmail.com>
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
*/
|
|
|
|
class SchemaValidator
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var EntityManager
|
|
|
|
*/
|
|
|
|
private $em;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param EntityManager $em
|
|
|
|
*/
|
|
|
|
public function __construct(EntityManager $em)
|
|
|
|
{
|
|
|
|
$this->em = $em;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-31 23:49:28 +04:00
|
|
|
* Checks the internal consistency of all mapping files.
|
2010-05-12 01:08:36 +04:00
|
|
|
*
|
2010-05-20 17:16:55 +04:00
|
|
|
* There are several checks that can't be done at runtime or are too expensive, which can be verified
|
2010-05-12 01:08:36 +04:00
|
|
|
* with this command. For example:
|
|
|
|
*
|
|
|
|
* 1. Check if a relation with "mappedBy" is actually connected to that specified field.
|
|
|
|
* 2. Check if "mappedBy" and "inversedBy" are consistent to each other.
|
|
|
|
* 3. Check if "referencedColumnName" attributes are really pointing to primary key columns.
|
2010-05-20 17:16:55 +04:00
|
|
|
* 4. Check if there are public properties that might cause problems with lazy loading.
|
2010-05-12 01:08:36 +04:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function validateMapping()
|
|
|
|
{
|
|
|
|
$errors = array();
|
|
|
|
$cmf = $this->em->getMetadataFactory();
|
|
|
|
$classes = $cmf->getAllMetadata();
|
|
|
|
|
|
|
|
foreach ($classes AS $class) {
|
2011-10-31 23:49:28 +04:00
|
|
|
if ($ce = $this->validateClass($class)) {
|
|
|
|
$errors[$class->name] = $ce;
|
|
|
|
}
|
|
|
|
}
|
2010-05-12 01:08:36 +04:00
|
|
|
|
2011-10-31 23:49:28 +04:00
|
|
|
return $errors;
|
|
|
|
}
|
2010-05-12 01:08:36 +04:00
|
|
|
|
2011-10-31 23:49:28 +04:00
|
|
|
/**
|
|
|
|
* Validate a single class of the current
|
|
|
|
*
|
|
|
|
* @param ClassMetadataInfo $class
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function validateClass(ClassMetadataInfo $class)
|
|
|
|
{
|
|
|
|
$ce = array();
|
|
|
|
$cmf = $this->em->getMetadataFactory();
|
2010-05-12 01:08:36 +04:00
|
|
|
|
2011-12-19 13:10:56 +04:00
|
|
|
foreach ($class->fieldMappings as $fieldName => $mapping) {
|
|
|
|
if (!Type::hasType($mapping['type'])) {
|
|
|
|
$ce[] = "The field '" . $class->name . "#" . $fieldName."' uses a non-existant type '" . $mapping['type'] . "'.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-31 23:49:28 +04:00
|
|
|
foreach ($class->associationMappings AS $fieldName => $assoc) {
|
2012-01-15 14:27:28 +04:00
|
|
|
if (!class_exists($assoc['targetEntity']) || $cmf->isTransient($assoc['targetEntity'])) {
|
|
|
|
$ce[] = "The target entity '" . $assoc['targetEntity'] . "' specified on " . $class->name . '#' . $fieldName . ' is unknown or not an entity.';
|
2011-10-31 23:49:28 +04:00
|
|
|
return $ce;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($assoc['mappedBy'] && $assoc['inversedBy']) {
|
|
|
|
$ce[] = "The association " . $class . "#" . $fieldName . " cannot be defined as both inverse and owning.";
|
|
|
|
}
|
|
|
|
|
|
|
|
$targetMetadata = $cmf->getMetadataFor($assoc['targetEntity']);
|
|
|
|
|
|
|
|
/* @var $assoc AssociationMapping */
|
|
|
|
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 ".
|
2012-01-15 14:27:28 +04:00
|
|
|
"'inversedBy=".$fieldName."' attribute.";
|
2011-10-31 23:49:28 +04:00
|
|
|
} else if ($targetMetadata->associationMappings[$assoc['mappedBy']]['inversedBy'] != $fieldName) {
|
|
|
|
$ce[] = "The mappings " . $class->name . "#" . $fieldName . " and " .
|
|
|
|
$assoc['targetEntity'] . "#" . $assoc['mappedBy'] . " are ".
|
|
|
|
"incosistent with each other.";
|
2010-05-12 01:08:36 +04:00
|
|
|
}
|
2011-10-31 23:49:28 +04:00
|
|
|
}
|
2010-05-12 01:08:36 +04:00
|
|
|
|
2011-10-31 23:49:28 +04:00
|
|
|
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.";
|
2010-05-12 01:08:36 +04:00
|
|
|
}
|
2011-10-31 23:49:28 +04:00
|
|
|
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.";
|
|
|
|
}
|
2011-12-18 02:27:39 +04:00
|
|
|
|
|
|
|
// Verify inverse side/owning side match each other
|
|
|
|
$targetAssoc = $targetMetadata->associationMappings[$assoc['inversedBy']];
|
2011-12-18 02:38:39 +04:00
|
|
|
if ($assoc['type'] == ClassMetadataInfo::ONE_TO_ONE && $targetAssoc['type'] !== ClassMetadataInfo::ONE_TO_ONE){
|
2011-12-18 02:27:39 +04:00
|
|
|
$ce[] = "If association " . $class->name . "#" . $fieldName . " is one-to-one, then the inversed " .
|
|
|
|
"side " . $targetMetadata->name . "#" . $assoc['inversedBy'] . " has to be one-to-one as well.";
|
2011-12-18 02:38:39 +04:00
|
|
|
} else if ($assoc['type'] == ClassMetadataInfo::MANY_TO_ONE && $targetAssoc['type'] !== ClassMetadataInfo::ONE_TO_MANY){
|
2011-12-18 02:27:39 +04:00
|
|
|
$ce[] = "If association " . $class->name . "#" . $fieldName . " is many-to-one, then the inversed " .
|
|
|
|
"side " . $targetMetadata->name . "#" . $assoc['inversedBy'] . " has to be one-to-many.";
|
2011-12-18 02:38:39 +04:00
|
|
|
} else if ($assoc['type'] == ClassMetadataInfo::MANY_TO_MANY && $targetAssoc['type'] !== ClassMetadataInfo::MANY_TO_MANY){
|
2011-12-18 02:27:39 +04:00
|
|
|
$ce[] = "If association " . $class->name . "#" . $fieldName . " is many-to-many, then the inversed " .
|
|
|
|
"side " . $targetMetadata->name . "#" . $assoc['inversedBy'] . " has to be many-to-many as well.";
|
|
|
|
}
|
2011-10-31 23:49:28 +04:00
|
|
|
}
|
2010-05-12 01:08:36 +04:00
|
|
|
|
2011-10-31 23:49:28 +04:00
|
|
|
if ($assoc['isOwningSide']) {
|
|
|
|
if ($assoc['type'] == ClassMetadataInfo::MANY_TO_MANY) {
|
2012-01-15 14:27:28 +04:00
|
|
|
$identifierColumns = $class->getIdentifierColumnNames();
|
2011-10-31 23:49:28 +04:00
|
|
|
foreach ($assoc['joinTable']['joinColumns'] AS $joinColumn) {
|
2012-01-15 14:27:28 +04:00
|
|
|
if (!in_array($joinColumn['referencedColumnName'], $identifierColumns)) {
|
2011-10-31 23:49:28 +04:00
|
|
|
$ce[] = "The referenced column name '" . $joinColumn['referencedColumnName'] . "' " .
|
2012-01-15 14:27:28 +04:00
|
|
|
"has to be a primary key column on the target entity class '".$class->name."'.";
|
|
|
|
break;
|
2011-10-31 23:49:28 +04:00
|
|
|
}
|
|
|
|
}
|
2012-01-15 14:27:28 +04:00
|
|
|
|
|
|
|
$identifierColumns = $targetMetadata->getIdentifierColumnNames();
|
2011-10-31 23:49:28 +04:00
|
|
|
foreach ($assoc['joinTable']['inverseJoinColumns'] AS $inverseJoinColumn) {
|
2012-01-15 14:27:28 +04:00
|
|
|
if (!in_array($inverseJoinColumn['referencedColumnName'], $identifierColumns)) {
|
|
|
|
$ce[] = "The referenced column name '" . $joinColumn['referencedColumnName'] . "' " .
|
|
|
|
"has to be a primary key column on the target entity class '".$targetMetadata->name."'.";
|
2011-10-31 23:49:28 +04:00
|
|
|
break;
|
2011-10-15 22:31:56 +04:00
|
|
|
}
|
2011-10-31 23:49:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count($targetMetadata->getIdentifierColumnNames()) != count($assoc['joinTable']['inverseJoinColumns'])) {
|
|
|
|
$ce[] = "The inverse join columns of the many-to-many table '" . $assoc['joinTable']['name'] . "' " .
|
|
|
|
"have to contain to ALL identifier columns of the target entity '". $targetMetadata->name . "', " .
|
2011-11-13 18:45:06 +04:00
|
|
|
"however '" . implode(", ", array_diff($targetMetadata->getIdentifierColumnNames(), array_values($assoc['relationToTargetKeyColumns']))) .
|
2011-10-31 23:49:28 +04:00
|
|
|
"' are missing.";
|
|
|
|
}
|
2011-10-15 22:31:56 +04:00
|
|
|
|
2011-10-31 23:49:28 +04:00
|
|
|
if (count($class->getIdentifierColumnNames()) != count($assoc['joinTable']['joinColumns'])) {
|
|
|
|
$ce[] = "The join columns of the many-to-many table '" . $assoc['joinTable']['name'] . "' " .
|
|
|
|
"have to contain to ALL identifier columns of the source entity '". $class->name . "', " .
|
2011-11-13 18:45:06 +04:00
|
|
|
"however '" . implode(", ", array_diff($class->getIdentifierColumnNames(), array_values($assoc['relationToSourceKeyColumns']))) .
|
2011-10-31 23:49:28 +04:00
|
|
|
"' are missing.";
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if ($assoc['type'] & ClassMetadataInfo::TO_ONE) {
|
2012-01-15 14:27:28 +04:00
|
|
|
$identifierColumns = $targetMetadata->getIdentifierColumnNames();
|
2011-10-31 23:49:28 +04:00
|
|
|
foreach ($assoc['joinColumns'] AS $joinColumn) {
|
2012-01-15 14:27:28 +04:00
|
|
|
if (!in_array($joinColumn['referencedColumnName'], $identifierColumns)) {
|
2011-10-31 23:49:28 +04:00
|
|
|
$ce[] = "The referenced column name '" . $joinColumn['referencedColumnName'] . "' " .
|
2012-01-15 14:27:28 +04:00
|
|
|
"has to be a primary key column on the target entity class '".$targetMetadata->name."'.";
|
2011-10-15 22:31:56 +04:00
|
|
|
}
|
2010-05-12 01:08:36 +04:00
|
|
|
}
|
2010-08-08 13:39:44 +04:00
|
|
|
|
2012-01-15 14:27:28 +04:00
|
|
|
if (count($identifierColumns) != count($assoc['joinColumns'])) {
|
2011-11-13 18:45:06 +04:00
|
|
|
$ids = array();
|
|
|
|
foreach ($assoc['joinColumns'] AS $joinColumn) {
|
|
|
|
$ids[] = $joinColumn['name'];
|
|
|
|
}
|
|
|
|
|
2011-10-31 23:49:28 +04:00
|
|
|
$ce[] = "The join columns of the association '" . $assoc['fieldName'] . "' " .
|
2012-01-15 14:27:28 +04:00
|
|
|
"have to match to ALL identifier columns of the target entity '". $class->name . "', " .
|
|
|
|
"however '" . implode(", ", array_diff($targetMetadata->getIdentifierColumnNames(), $ids)) .
|
2011-10-31 23:49:28 +04:00
|
|
|
"' are missing.";
|
2010-08-08 13:39:44 +04:00
|
|
|
}
|
|
|
|
}
|
2010-05-20 17:16:55 +04:00
|
|
|
}
|
2010-05-12 01:08:36 +04:00
|
|
|
|
2011-10-31 23:49:28 +04:00
|
|
|
if (isset($assoc['orderBy']) && $assoc['orderBy'] !== null) {
|
|
|
|
foreach ($assoc['orderBy'] AS $orderField => $orientation) {
|
|
|
|
if (!$targetMetadata->hasField($orderField)) {
|
|
|
|
$ce[] = "The association " . $class->name."#".$fieldName." is ordered by a foreign field " .
|
|
|
|
$orderField . " that is not a field on the target entity " . $targetMetadata->name;
|
|
|
|
}
|
2010-05-21 01:50:59 +04:00
|
|
|
}
|
2010-07-10 11:13:02 +04:00
|
|
|
}
|
2011-10-31 23:49:28 +04:00
|
|
|
}
|
2010-07-10 11:13:02 +04:00
|
|
|
|
2011-10-31 23:49:28 +04:00
|
|
|
foreach ($class->reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $publicAttr) {
|
|
|
|
if ($publicAttr->isStatic()) {
|
|
|
|
continue;
|
2010-05-20 17:16:55 +04:00
|
|
|
}
|
2011-10-31 23:49:28 +04:00
|
|
|
$ce[] = "Field '".$publicAttr->getName()."' in class '".$class->name."' must be private ".
|
|
|
|
"or protected. Public fields may break lazy-loading.";
|
|
|
|
}
|
2010-05-20 17:16:55 +04:00
|
|
|
|
2011-10-31 23:49:28 +04:00
|
|
|
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.";
|
2010-05-12 01:08:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-31 23:49:28 +04:00
|
|
|
return $ce;
|
2010-05-12 01:08:36 +04:00
|
|
|
}
|
|
|
|
|
2012-01-15 14:27:28 +04:00
|
|
|
/**
|
|
|
|
* @param string $columnName
|
|
|
|
* @param ClassMetadataInfo $class
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function columnExistsOnEntity($columnName, $class)
|
|
|
|
{
|
|
|
|
if (isset($class->fieldNames[$columnName])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
foreach ($class->associationMappings as $assoc) {
|
|
|
|
if ($assoc['isOwningSide']) {
|
|
|
|
foreach ($assoc['joinColumns'] as $columnMapping) {
|
|
|
|
if ($columnMapping['name'] == $columnName) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-12 01:08:36 +04:00
|
|
|
/**
|
|
|
|
* Check if the Database Schema is in sync with the current metadata state.
|
2010-05-20 17:16:55 +04:00
|
|
|
*
|
2010-05-12 01:08:36 +04:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function schemaInSyncWithMetadata()
|
|
|
|
{
|
|
|
|
$schemaTool = new SchemaTool($this->em);
|
|
|
|
|
|
|
|
$allMetadata = $this->em->getMetadataFactory()->getAllMetadata();
|
2011-11-01 02:12:52 +04:00
|
|
|
return (count($schemaTool->getUpdateSchemaSql($allMetadata, true)) == 0);
|
2010-05-12 01:08:36 +04:00
|
|
|
}
|
|
|
|
}
|