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.
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>.
20 namespace Doctrine\ORM\Tools;
22 use Doctrine\ORM\EntityManager;
23 use Doctrine\ORM\Mapping\ClassMetadataInfo;
24 use Doctrine\DBAL\Types\Type;
27 * Performs strict validation of the mapping schema
29 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
30 * @link www.doctrine-project.com
32 * @author Benjamin Eberlei <kontakt@beberlei.de>
33 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
34 * @author Jonathan Wage <jonwage@gmail.com>
35 * @author Roman Borschel <roman@code-factory.org>
45 * @param EntityManager $em
47 public function __construct(EntityManager $em)
53 * Checks the internal consistency of all mapping files.
55 * There are several checks that can't be done at runtime or are too expensive, which can be verified
56 * with this command. For example:
58 * 1. Check if a relation with "mappedBy" is actually connected to that specified field.
59 * 2. Check if "mappedBy" and "inversedBy" are consistent to each other.
60 * 3. Check if "referencedColumnName" attributes are really pointing to primary key columns.
61 * 4. Check if there are public properties that might cause problems with lazy loading.
65 public function validateMapping()
68 $cmf = $this->em->getMetadataFactory();
69 $classes = $cmf->getAllMetadata();
71 foreach ($classes as $class) {
72 if ($ce = $this->validateClass($class)) {
73 $errors[$class->name] = $ce;
81 * Validate a single class of the current
83 * @param ClassMetadataInfo $class
86 public function validateClass(ClassMetadataInfo $class)
89 $cmf = $this->em->getMetadataFactory();
91 foreach ($class->fieldMappings as $fieldName => $mapping) {
92 if (!Type::hasType($mapping['type'])) {
93 $ce[] = "The field '" . $class->name . "#" . $fieldName."' uses a non-existant type '" . $mapping['type'] . "'.";
97 foreach ($class->associationMappings as $fieldName => $assoc) {
98 if (!class_exists($assoc['targetEntity']) || $cmf->isTransient($assoc['targetEntity'])) {
99 $ce[] = "The target entity '" . $assoc['targetEntity'] . "' specified on " . $class->name . '#' . $fieldName . ' is unknown or not an entity.';
103 if ($assoc['mappedBy'] && $assoc['inversedBy']) {
104 $ce[] = "The association " . $class . "#" . $fieldName . " cannot be defined as both inverse and owning.";
107 $targetMetadata = $cmf->getMetadataFor($assoc['targetEntity']);
109 if (isset($assoc['id']) && $targetMetadata->containsForeignIdentifier) {
110 $ce[] = "Cannot map association '" . $class->name. "#". $fieldName ." as identifier, because " .
111 "the target entity '". $targetMetadata->name . "' also maps an association as identifier.";
114 /* @var $assoc AssociationMapping */
115 if ($assoc['mappedBy']) {
116 if ($targetMetadata->hasField($assoc['mappedBy'])) {
117 $ce[] = "The association " . $class->name . "#" . $fieldName . " refers to the owning side ".
118 "field " . $assoc['targetEntity'] . "#" . $assoc['mappedBy'] . " which is not defined as association.";
120 if (!$targetMetadata->hasAssociation($assoc['mappedBy'])) {
121 $ce[] = "The association " . $class->name . "#" . $fieldName . " refers to the owning side ".
122 "field " . $assoc['targetEntity'] . "#" . $assoc['mappedBy'] . " which does not exist.";
123 } else if ($targetMetadata->associationMappings[$assoc['mappedBy']]['inversedBy'] == null) {
124 $ce[] = "The field " . $class->name . "#" . $fieldName . " is on the inverse side of a ".
125 "bi-directional relationship, but the specified mappedBy association on the target-entity ".
126 $assoc['targetEntity'] . "#" . $assoc['mappedBy'] . " does not contain the required ".
127 "'inversedBy=".$fieldName."' attribute.";
128 } else if ($targetMetadata->associationMappings[$assoc['mappedBy']]['inversedBy'] != $fieldName) {
129 $ce[] = "The mappings " . $class->name . "#" . $fieldName . " and " .
130 $assoc['targetEntity'] . "#" . $assoc['mappedBy'] . " are ".
131 "incosistent with each other.";
135 if ($assoc['inversedBy']) {
136 if ($targetMetadata->hasField($assoc['inversedBy'])) {
137 $ce[] = "The association " . $class->name . "#" . $fieldName . " refers to the inverse side ".
138 "field " . $assoc['targetEntity'] . "#" . $assoc['inversedBy'] . " which is not defined as association.";
140 if (!$targetMetadata->hasAssociation($assoc['inversedBy'])) {
141 $ce[] = "The association " . $class->name . "#" . $fieldName . " refers to the inverse side ".
142 "field " . $assoc['targetEntity'] . "#" . $assoc['inversedBy'] . " which does not exist.";
143 } else if ($targetMetadata->associationMappings[$assoc['inversedBy']]['mappedBy'] == null) {
144 $ce[] = "The field " . $class->name . "#" . $fieldName . " is on the owning side of a ".
145 "bi-directional relationship, but the specified mappedBy association on the target-entity ".
146 $assoc['targetEntity'] . "#" . $assoc['mappedBy'] . " does not contain the required ".
147 "'inversedBy' attribute.";
148 } else if ($targetMetadata->associationMappings[$assoc['inversedBy']]['mappedBy'] != $fieldName) {
149 $ce[] = "The mappings " . $class->name . "#" . $fieldName . " and " .
150 $assoc['targetEntity'] . "#" . $assoc['inversedBy'] . " are ".
151 "incosistent with each other.";
154 // Verify inverse side/owning side match each other
155 if (array_key_exists($assoc['inversedBy'], $targetMetadata->associationMappings)) {
156 $targetAssoc = $targetMetadata->associationMappings[$assoc['inversedBy']];
157 if ($assoc['type'] == ClassMetadataInfo::ONE_TO_ONE && $targetAssoc['type'] !== ClassMetadataInfo::ONE_TO_ONE){
158 $ce[] = "If association " . $class->name . "#" . $fieldName . " is one-to-one, then the inversed " .
159 "side " . $targetMetadata->name . "#" . $assoc['inversedBy'] . " has to be one-to-one as well.";
160 } else if ($assoc['type'] == ClassMetadataInfo::MANY_TO_ONE && $targetAssoc['type'] !== ClassMetadataInfo::ONE_TO_MANY){
161 $ce[] = "If association " . $class->name . "#" . $fieldName . " is many-to-one, then the inversed " .
162 "side " . $targetMetadata->name . "#" . $assoc['inversedBy'] . " has to be one-to-many.";
163 } else if ($assoc['type'] == ClassMetadataInfo::MANY_TO_MANY && $targetAssoc['type'] !== ClassMetadataInfo::MANY_TO_MANY){
164 $ce[] = "If association " . $class->name . "#" . $fieldName . " is many-to-many, then the inversed " .
165 "side " . $targetMetadata->name . "#" . $assoc['inversedBy'] . " has to be many-to-many as well.";
170 if ($assoc['isOwningSide']) {
171 if ($assoc['type'] == ClassMetadataInfo::MANY_TO_MANY) {
172 $identifierColumns = $class->getIdentifierColumnNames();
173 foreach ($assoc['joinTable']['joinColumns'] as $joinColumn) {
174 if (!in_array($joinColumn['referencedColumnName'], $identifierColumns)) {
175 $ce[] = "The referenced column name '" . $joinColumn['referencedColumnName'] . "' " .
176 "has to be a primary key column on the target entity class '".$class->name."'.";
181 $identifierColumns = $targetMetadata->getIdentifierColumnNames();
182 foreach ($assoc['joinTable']['inverseJoinColumns'] as $inverseJoinColumn) {
183 if (!in_array($inverseJoinColumn['referencedColumnName'], $identifierColumns)) {
184 $ce[] = "The referenced column name '" . $joinColumn['referencedColumnName'] . "' " .
185 "has to be a primary key column on the target entity class '".$targetMetadata->name."'.";
190 if (count($targetMetadata->getIdentifierColumnNames()) != count($assoc['joinTable']['inverseJoinColumns'])) {
191 $ce[] = "The inverse join columns of the many-to-many table '" . $assoc['joinTable']['name'] . "' " .
192 "have to contain to ALL identifier columns of the target entity '". $targetMetadata->name . "', " .
193 "however '" . implode(", ", array_diff($targetMetadata->getIdentifierColumnNames(), array_values($assoc['relationToTargetKeyColumns']))) .
197 if (count($class->getIdentifierColumnNames()) != count($assoc['joinTable']['joinColumns'])) {
198 $ce[] = "The join columns of the many-to-many table '" . $assoc['joinTable']['name'] . "' " .
199 "have to contain to ALL identifier columns of the source entity '". $class->name . "', " .
200 "however '" . implode(", ", array_diff($class->getIdentifierColumnNames(), array_values($assoc['relationToSourceKeyColumns']))) .
204 } else if ($assoc['type'] & ClassMetadataInfo::TO_ONE) {
205 $identifierColumns = $targetMetadata->getIdentifierColumnNames();
206 foreach ($assoc['joinColumns'] as $joinColumn) {
207 if (!in_array($joinColumn['referencedColumnName'], $identifierColumns)) {
208 $ce[] = "The referenced column name '" . $joinColumn['referencedColumnName'] . "' " .
209 "has to be a primary key column on the target entity class '".$targetMetadata->name."'.";
213 if (count($identifierColumns) != count($assoc['joinColumns'])) {
215 foreach ($assoc['joinColumns'] as $joinColumn) {
216 $ids[] = $joinColumn['name'];
219 $ce[] = "The join columns of the association '" . $assoc['fieldName'] . "' " .
220 "have to match to ALL identifier columns of the target entity '". $class->name . "', " .
221 "however '" . implode(", ", array_diff($targetMetadata->getIdentifierColumnNames(), $ids)) .
227 if (isset($assoc['orderBy']) && $assoc['orderBy'] !== null) {
228 foreach ($assoc['orderBy'] as $orderField => $orientation) {
229 if (!$targetMetadata->hasField($orderField)) {
230 $ce[] = "The association " . $class->name."#".$fieldName." is ordered by a foreign field " .
231 $orderField . " that is not a field on the target entity " . $targetMetadata->name;
237 foreach ($class->reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $publicAttr) {
238 if ($publicAttr->isStatic()) {
241 $ce[] = "Field '".$publicAttr->getName()."' in class '".$class->name."' must be private ".
242 "or protected. Public fields may break lazy-loading.";
245 foreach ($class->subClasses as $subClass) {
246 if (!in_array($class->name, class_parents($subClass))) {
247 $ce[] = "According to the discriminator map class '" . $subClass . "' has to be a child ".
248 "of '" . $class->name . "' but these entities are not related through inheritance.";
256 * @param string $columnName
257 * @param ClassMetadataInfo $class
260 private function columnExistsOnEntity($columnName, $class)
262 if (isset($class->fieldNames[$columnName])) {
265 foreach ($class->associationMappings as $assoc) {
266 if ($assoc['isOwningSide']) {
267 foreach ($assoc['joinColumns'] as $columnMapping) {
268 if ($columnMapping['name'] == $columnName) {
278 * Check if the Database Schema is in sync with the current metadata state.
282 public function schemaInSyncWithMetadata()
284 $schemaTool = new SchemaTool($this->em);
286 $allMetadata = $this->em->getMetadataFactory()->getAllMetadata();
287 return (count($schemaTool->getUpdateSchemaSql($allMetadata, true)) == 0);