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\Event\LoadClassMetadataEventArgs;
23 use Doctrine\ORM\Mapping\ClassMetadata;
26 * ResolveTargetEntityListener
28 * Mechanism to overwrite interfaces or classes specified as association
31 * @author Benjamin Eberlei <kontakt@beberlei.de>
34 class ResolveTargetEntityListener
39 private $resolveTargetEntities = array();
42 * Add a target-entity class name to resolve to a new class name.
44 * @param string $originalEntity
45 * @param string $newEntity
46 * @param array $mapping
49 public function addResolveTargetEntity($originalEntity, $newEntity, array $mapping)
51 $mapping['targetEntity'] = ltrim($newEntity, "\\");
52 $this->resolveTargetEntities[ltrim($originalEntity, "\\")] = $mapping;
56 * Process event and resolve new target entity names.
58 * @param LoadClassMetadataEventArgs $args
61 public function loadClassMetadata(LoadClassMetadataEventArgs $args)
63 $cm = $args->getClassMetadata();
64 foreach ($cm->associationMappings as $mapping) {
65 if (isset($this->resolveTargetEntities[$mapping['targetEntity']])) {
66 $this->remapAssociation($cm, $mapping);
71 private function remapAssociation($classMetadata, $mapping)
73 $newMapping = $this->resolveTargetEntities[$mapping['targetEntity']];
74 $newMapping = array_replace_recursive($mapping, $newMapping);
75 $newMapping['fieldName'] = $mapping['fieldName'];
76 unset($classMetadata->associationMappings[$mapping['fieldName']]);
78 switch ($mapping['type']) {
79 case ClassMetadata::MANY_TO_MANY:
80 $classMetadata->mapManyToMany($newMapping);
82 case ClassMetadata::MANY_TO_ONE:
83 $classMetadata->mapManyToOne($newMapping);
85 case ClassMetadata::ONE_TO_MANY:
86 $classMetadata->mapOneToMany($newMapping);
88 case ClassMetadata::ONE_TO_ONE:
89 $classMetadata->mapOneToOne($newMapping);