OneToManyMapping.php 4.83 KB
Newer Older
romanb's avatar
romanb committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
/*
 *  $Id$
 *
 * 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
19
 * <http://www.doctrine-project.org>.
romanb's avatar
romanb committed
20 21
 */

22
namespace Doctrine\ORM\Mapping;
romanb's avatar
romanb committed
23 24 25 26 27 28 29 30 31

/**
 * Represents a one-to-many mapping.
 * 
 * NOTE: One-to-many mappings can currently not be uni-directional (one -> many).
 * They must either be bidirectional (one <-> many) or unidirectional (many -> one).
 * In other words, the many-side MUST be the owning side and the one-side MUST be
 * the inverse side.
 *
32 33 34 35 36 37 38 39
 * <b>IMPORTANT NOTE:</b>
 *
 * The fields of this class are only public for 2 reasons:
 * 1) To allow fast, internal READ access.
 * 2) To drastically reduce the size of a serialized instance (private/protected members
 *    get the whole class name, namespace inclusive, prepended to every property in
 *    the serialized representation).
 *
romanb's avatar
romanb committed
40
 * @author Roman Borschel <roman@code-factory.org>
41
 * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
romanb's avatar
romanb committed
42 43
 * @since 2.0
 */
44
class OneToManyMapping extends AssociationMapping
romanb's avatar
romanb committed
45 46
{
    /** Whether to delete orphaned elements (removed from the collection) */
47
    public $orphanRemoval = false;
romanb's avatar
romanb committed
48 49
    /** FUTURE: The key column mapping, if any. The key column holds the keys of the Collection. */
    //public $keyColumn;
romanb's avatar
romanb committed
50
    
51 52 53 54 55 56 57
    /** 
     * TODO: Allow any combination of source/target columns in lazy loading.
     * What is supported now is primary key (that can spread on multiple fields)
     * pointed to foreign keys on the target
    public $targetColumns;
     */

romanb's avatar
romanb committed
58
    /**
59
     * Initializes a new OneToManyMapping.
romanb's avatar
romanb committed
60
     *
61
     * @param array $mapping  The mapping information.
romanb's avatar
romanb committed
62 63 64 65 66 67 68
     */
    public function __construct(array $mapping)
    {
        parent::__construct($mapping);
    }
    
    /**
69
     * Validates and completes the mapping.
romanb's avatar
romanb committed
70 71 72 73 74 75 76 77
     *
     * @param array $mapping The mapping to validate and complete.
     * @return array The validated and completed mapping.
     * @override
     */
    protected function _validateAndCompleteMapping(array $mapping)
    {
        parent::_validateAndCompleteMapping($mapping);
78

romanb's avatar
romanb committed
79 80
        // one-side MUST be inverse (must have mappedBy)
        if ( ! isset($mapping['mappedBy'])) {
81
            throw MappingException::oneToManyRequiresMappedBy($mapping['fieldName']);
romanb's avatar
romanb committed
82 83
        }
        
84 85
        $this->orphanRemoval = isset($mapping['orphanRemoval']) ?
                (bool) $mapping['orphanRemoval'] : false;
romanb's avatar
romanb committed
86 87 88 89 90 91 92 93 94
    }
    
    /**
     * Whether orphaned elements (removed from the collection) should be deleted.
     *
     * @return boolean TRUE if orphaned elements should be deleted, FALSE otherwise.
     */
    public function shouldDeleteOrphans()
    {
95
        return $this->deleteOrphans;
romanb's avatar
romanb committed
96 97 98
    }
    
    /**
99
     * {@inheritdoc}
romanb's avatar
romanb committed
100 101 102 103 104 105 106
     *
     * @override
     */
    public function isOneToMany()
    {
        return true;
    }
107 108 109 110 111 112 113 114 115 116
    
    /**
     * 
     * 
     * @param $sourceEntity
     * @param $targetCollection
     * @param $em
     * @param $joinColumnValues
     * @return unknown_type
     */
117
    public function load($sourceEntity, $targetCollection, $em, array $joinColumnValues = array())
118
    {
119 120 121
        $persister = $em->getUnitOfWork()->getEntityPersister($this->targetEntityName);
        // a one-to-many is always inverse (does not have foreign key)
        $sourceClass = $em->getClassMetadata($this->sourceEntityName);
122
        $owningAssoc = $em->getClassMetadata($this->targetEntityName)->associationMappings[$this->mappedByFieldName];
123 124 125 126
        // TRICKY: since the association is specular source and target are flipped
        foreach ($owningAssoc->getTargetToSourceKeyColumns() as $sourceKeyColumn => $targetKeyColumn) {
            // getting id
            if (isset($sourceClass->reflFields[$sourceKeyColumn])) {
127
                $conditions[$targetKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
128 129 130 131 132
            } else {
                $conditions[$targetKeyColumn] = $joinColumnValues[$sourceKeyColumn];
            }
        }

133
        $persister->loadOneToManyCollection($conditions, $targetCollection);
134
    }
135
}