OneToOneMapping.php 8.33 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

/**
 * A one-to-one mapping describes a uni-directional mapping from one entity 
 * to another entity.
 *
28 29 30 31 32 33 34 35
 * <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
36 37
 * @since 2.0
 * @author Roman Borschel <roman@code-factory.org>
38
 * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
romanb's avatar
romanb committed
39
 */
40
class OneToOneMapping extends AssociationMapping
romanb's avatar
romanb committed
41 42 43 44 45 46
{
    /**
     * Maps the source foreign/primary key columns to the target primary/foreign key columns.
     * i.e. source.id (pk) => target.user_id (fk).
     * Reverse mapping of _targetToSourceKeyColumns.
     */
47
    public $sourceToTargetKeyColumns = array();
romanb's avatar
romanb committed
48 49 50 51 52 53

    /**
     * Maps the target primary/foreign key columns to the source foreign/primary key columns.
     * i.e. target.user_id (fk) => source.id (pk).
     * Reverse mapping of _sourceToTargetKeyColumns.
     */
54
    public $targetToSourceKeyColumns = array();
romanb's avatar
romanb committed
55 56 57 58 59 60
    
    /**
     * Whether to delete orphaned elements (when nulled out, i.e. $foo->other = null)
     * 
     * @var boolean
     */
61
    public $orphanRemoval = false;
62 63 64 65 66 67

    /**
     * The join column definitions.
     *
     * @var array
     */
68
    public $joinColumns = array();
romanb's avatar
romanb committed
69
    
70 71 72 73 74 75
    /**
     * A map of join column names to field names that are used in cases
     * when the join columns are fetched as part of the query result.
     * 
     * @var array
     */
76 77
    public $joinColumnFieldNames = array();
    
romanb's avatar
romanb committed
78 79 80 81 82 83 84 85 86 87 88
    /**
     * Creates a new OneToOneMapping.
     *
     * @param array $mapping  The mapping info.
     */
    public function __construct(array $mapping)
    {
        parent::__construct($mapping);
    }
    
    /**
89
     * {@inheritdoc}
romanb's avatar
romanb committed
90 91 92 93 94 95 96 97 98
     *
     * @param array $mapping  The mapping to validate & complete.
     * @return array  The validated & completed mapping.
     * @override
     */
    protected function _validateAndCompleteMapping(array $mapping)
    {
        parent::_validateAndCompleteMapping($mapping);
        
99 100 101 102 103
        if (isset($mapping['joinColumns']) && $mapping['joinColumns']) {
            $this->isOwningSide = true;
        }
        
        if ($this->isOwningSide) {
romanb's avatar
romanb committed
104
            if ( ! isset($mapping['joinColumns'])) {
105
                throw MappingException::invalidMapping($this->sourceFieldName);
106
            }
107 108 109 110 111
            foreach ($mapping['joinColumns'] as &$joinColumn) {
                if ($joinColumn['name'][0] == '`') {
                    $joinColumn['name'] = trim($joinColumn['name'], '`');
                    $joinColumn['quoted'] = true;
                }
112
                $this->sourceToTargetKeyColumns[$joinColumn['name']] = $joinColumn['referencedColumnName'];
113 114
                $this->joinColumnFieldNames[$joinColumn['name']] = isset($joinColumn['fieldName'])
                        ? $joinColumn['fieldName'] : $joinColumn['name'];
romanb's avatar
romanb committed
115
            }
116
            $this->joinColumns = $mapping['joinColumns'];
117
            $this->targetToSourceKeyColumns = array_flip($this->sourceToTargetKeyColumns);
romanb's avatar
romanb committed
118 119
        }
        
120 121
        $this->orphanRemoval = isset($mapping['orphanRemoval']) ?
                (bool) $mapping['orphanRemoval'] : false;
romanb's avatar
romanb committed
122
        
123 124 125 126
        /*if ($this->isOptional) {
            $this->fetchMode = self::FETCH_EAGER;
        }*/
        
romanb's avatar
romanb committed
127 128
        return $mapping;
    }
129 130 131 132 133 134 135 136

    /**
     * Gets the join column definitions for this mapping.
     *
     * @return array
     */
    public function getJoinColumns()
    {
137
        return $this->joinColumns;
138
    }
romanb's avatar
romanb committed
139 140 141 142
    
    /**
     * Gets the source-to-target key column mapping.
     *
143
     * @return array
romanb's avatar
romanb committed
144 145 146
     */
    public function getSourceToTargetKeyColumns()
    {
147
        return $this->sourceToTargetKeyColumns;
romanb's avatar
romanb committed
148 149 150 151 152
    }
    
    /**
     * Gets the target-to-source key column mapping.
     *
153
     * @return array
romanb's avatar
romanb committed
154 155 156
     */
    public function getTargetToSourceKeyColumns()
    {
157
        return $this->targetToSourceKeyColumns;
romanb's avatar
romanb committed
158 159 160
    }
    
    /**
161
     * {@inheritdoc}
romanb's avatar
romanb committed
162 163 164 165 166 167 168 169 170
     *
     * @return boolean
     * @override
     */
    public function isOneToOne()
    {
        return true;
    }
    
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    /**
     * Gets the (possibly quoted) column name of a join column that is safe to use
     * in an SQL statement.
     * 
     * @param string $joinColumn
     * @param AbstractPlatform $platform
     * @return string
     */
    public function getQuotedJoinColumnName($joinColumn, $platform)
    {
        return isset($this->joinColumns[$joinColumn]['quoted']) ?
                $platform->quoteIdentifier($joinColumn) :
                $joinColumn;
    }
    
romanb's avatar
romanb committed
186
    /**
187
     * {@inheritdoc}
romanb's avatar
romanb committed
188
     *
189 190
     * @param object $sourceEntity      the entity source of this association
     * @param object $targetEntity      the entity to load data in
191
     * @param EntityManager $em
192
     * @param array $joinColumnValues  values of the join columns of $sourceEntity. There are no fields
193
     *                                 to store this data in $sourceEntity
romanb's avatar
romanb committed
194
     */
195
    public function load($sourceEntity, $targetEntity, $em, array $joinColumnValues = array())
romanb's avatar
romanb committed
196
    {
197 198
        $sourceClass = $em->getClassMetadata($this->sourceEntityName);
        $targetClass = $em->getClassMetadata($this->targetEntityName);
romanb's avatar
romanb committed
199
        
200 201
        $conditions = array();

202 203
        if ($this->isOwningSide) {
            foreach ($this->sourceToTargetKeyColumns as $sourceKeyColumn => $targetKeyColumn) {
204 205
                // getting customer_id
                if (isset($sourceClass->reflFields[$sourceKeyColumn])) {
206
                    $conditions[$targetKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
207 208 209
                } else {
                    $conditions[$targetKeyColumn] = $joinColumnValues[$sourceKeyColumn];
                }
210
            }
211 212 213
            
            $targetEntity = $em->getUnitOfWork()->getEntityPersister($this->targetEntityName)->load($conditions, $targetEntity);
            
214
            if ($targetEntity !== null && $targetClass->hasInverseAssociationMapping($this->sourceEntityName, $this->sourceFieldName)) {
215
                $targetClass->setFieldValue($targetEntity,
216
                        $targetClass->inverseMappings[$this->sourceEntityName][$this->sourceFieldName]->sourceFieldName,
217
                        $sourceEntity);
218 219
            }
        } else {
220
            $owningAssoc = $em->getClassMetadata($this->targetEntityName)->getAssociationMapping($this->mappedByFieldName);
221
            // TRICKY: since the association is specular source and target are flipped
222
            foreach ($owningAssoc->targetToSourceKeyColumns as $sourceKeyColumn => $targetKeyColumn) {
223
                // getting id
224
                if (isset($sourceClass->reflFields[$sourceKeyColumn])) {
225
                    $conditions[$targetKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
226
                } else {
227
                    $conditions[$targetKeyColumn] = $joinColumnValues[$sourceKeyColumn];
228
                }
229
            }
230 231 232
            
            $targetEntity = $em->getUnitOfWork()->getEntityPersister($this->targetEntityName)->load($conditions, $targetEntity);
            
233
            $targetClass->setFieldValue($targetEntity, $this->mappedByFieldName, $sourceEntity);
romanb's avatar
romanb committed
234 235
        }
    }
236
}