OneToManyMapping.php 5.11 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
 * <b>IMPORTANT NOTE:</b>
 *
 * The fields of this class are only public for 2 reasons:
35
 * 1) To allow fast READ access.
36 37 38
 * 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).
39 40 41
 *    
 * Instances of this class are stored serialized in the metadata cache together with the
 * owning <tt>ClassMetadata</tt> instance.
42
 *
romanb's avatar
romanb committed
43
 * @author Roman Borschel <roman@code-factory.org>
44
 * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
romanb's avatar
romanb committed
45 46
 * @since 2.0
 */
47
class OneToManyMapping extends AssociationMapping
romanb's avatar
romanb committed
48
{
49 50 51 52 53
    /**
     * READ-ONLY: Whether to delete orphaned elements (removed from the collection)
     *
     * @var boolean
     */
54
    public $orphanRemoval = false;
55

romanb's avatar
romanb committed
56 57
    /** FUTURE: The key column mapping, if any. The key column holds the keys of the Collection. */
    //public $keyColumn;
58

59
    /**
60
     * READ-ONLY: Order this collection by the given SQL snippet.
61
     */
62
    public $orderBy;
63

romanb's avatar
romanb committed
64
    /**
65
     * Validates and completes the mapping.
romanb's avatar
romanb committed
66 67 68 69 70 71 72 73
     *
     * @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);
74

romanb's avatar
romanb committed
75
        // OneToMany-side MUST be inverse (must have mappedBy)
romanb's avatar
romanb committed
76
        if ( ! isset($mapping['mappedBy'])) {
77
            throw MappingException::oneToManyRequiresMappedBy($mapping['fieldName']);
romanb's avatar
romanb committed
78 79
        }
        
80
        //TODO: if orphanRemoval, cascade=remove is implicit!
81 82
        $this->orphanRemoval = isset($mapping['orphanRemoval']) ?
                (bool) $mapping['orphanRemoval'] : false;
83 84

        if (isset($mapping['orderBy'])) {
85 86
            if (!is_array($mapping['orderBy'])) {
                throw new \InvalidArgumentException("'orderBy' is expected to be an array, not ".gettype($mapping['orderBy']));
87
            }
88
            $this->orderBy = $mapping['orderBy'];
89
        }
romanb's avatar
romanb committed
90 91 92 93 94 95 96 97 98
    }
    
    /**
     * Whether orphaned elements (removed from the collection) should be deleted.
     *
     * @return boolean TRUE if orphaned elements should be deleted, FALSE otherwise.
     */
    public function shouldDeleteOrphans()
    {
99
        return $this->deleteOrphans;
romanb's avatar
romanb committed
100 101 102
    }
    
    /**
103
     * {@inheritdoc}
romanb's avatar
romanb committed
104 105 106 107 108 109 110
     *
     * @override
     */
    public function isOneToMany()
    {
        return true;
    }
111 112
    
    /**
romanb's avatar
romanb committed
113
     * Loads a one-to-many collection.
114
     * 
romanb's avatar
romanb committed
115 116 117 118 119
     * @param $sourceEntity The entity that owns the collection.
     * @param $targetCollection The collection to load/fill.
     * @param $em The EntityManager to use.
     * @param $joinColumnValues 
     * @return void
120
     */
121
    public function load($sourceEntity, $targetCollection, $em, array $joinColumnValues = array())
122
    {
123 124 125
        $persister = $em->getUnitOfWork()->getEntityPersister($this->targetEntityName);
        // a one-to-many is always inverse (does not have foreign key)
        $sourceClass = $em->getClassMetadata($this->sourceEntityName);
126
        $owningAssoc = $em->getClassMetadata($this->targetEntityName)->associationMappings[$this->mappedBy];
127
        // TRICKY: since the association is specular source and target are flipped
romanb's avatar
romanb committed
128
        foreach ($owningAssoc->targetToSourceKeyColumns as $sourceKeyColumn => $targetKeyColumn) {
129
            // getting id
romanb's avatar
romanb committed
130
            if (isset($sourceClass->fieldNames[$sourceKeyColumn])) {
131
                $conditions[$targetKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
132 133 134 135 136
            } else {
                $conditions[$targetKeyColumn] = $joinColumnValues[$sourceKeyColumn];
            }
        }

137
        $persister->loadOneToManyCollection($this, $conditions, $targetCollection);
138
    }
139
}