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

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

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

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

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

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

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

        if (isset($mapping['orderBy'])) {
84 85
            if (!is_array($mapping['orderBy'])) {
                throw new \InvalidArgumentException("'orderBy' is expected to be an array, not ".gettype($mapping['orderBy']));
86
            }
87
            $this->orderBy = $mapping['orderBy'];
88
        }
romanb's avatar
romanb committed
89 90 91 92 93 94 95 96 97
    }
    
    /**
     * Whether orphaned elements (removed from the collection) should be deleted.
     *
     * @return boolean TRUE if orphaned elements should be deleted, FALSE otherwise.
     */
    public function shouldDeleteOrphans()
    {
98
        return $this->deleteOrphans;
romanb's avatar
romanb committed
99 100 101
    }
    
    /**
102
     * {@inheritdoc}
romanb's avatar
romanb committed
103 104 105 106 107
     */
    public function isOneToMany()
    {
        return true;
    }
108 109
    
    /**
romanb's avatar
romanb committed
110
     * Loads a one-to-many collection.
111
     * 
romanb's avatar
romanb committed
112 113 114 115 116
     * @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
117
     * @todo Remove
118
     */
119
    public function load($sourceEntity, $targetCollection, $em, array $joinColumnValues = array())
120
    {
121
        $em->getUnitOfWork()->getEntityPersister($this->targetEntityName)->loadOneToManyCollection($this, $sourceEntity, $targetCollection);
122
    }
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

    /**
     * Determines which fields get serialized.
     *
     * It is only serialized what is necessary for best unserialization performance.
     * That means any metadata properties that are not set or empty or simply have
     * their default value are NOT serialized.
     *
     * @return array The names of all the fields that should be serialized.
     */
    public function __sleep()
    {
        $serialized = parent::__sleep();
        if ($this->orderBy) {
            $serialized[] = 'orderBy';
        }
        if ($this->orphanRemoval) {
            $serialized[] = 'orphanRemoval';
        }
        return $serialized;
    }
144
}