AssociationMapping.php 9.82 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

/**
 * Base class for association mappings.
 *
27 28 29 30 31 32 33 34
 * <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
35 36 37
 * @author Roman Borschel <roman@code-factory.org>
 * @since 2.0
 */
38
abstract class AssociationMapping
romanb's avatar
romanb committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
{
    const FETCH_MANUAL = 1;
    const FETCH_LAZY = 2;
    const FETCH_EAGER = 3;
    
    /**
     * Cascade types enumeration.
     *
     * @var array
     */
    protected static $_cascadeTypes = array(
        'all',
        'none',
        'save',
        'delete',
54 55
        'refresh',
        'merge'
romanb's avatar
romanb committed
56 57
    );
    
58 59 60 61 62
    public $cascades = array();
    public $isCascadeDelete;
    public $isCascadeSave;
    public $isCascadeRefresh;
    public $isCascadeMerge;
romanb's avatar
romanb committed
63 64 65 66 67 68
    
    /**
     * The fetch mode used for the association.
     *
     * @var integer
     */
69
    public $fetchMode = self::FETCH_MANUAL;
romanb's avatar
romanb committed
70 71 72 73 74 75 76
    
    /**
     * Flag that indicates whether the class that defines this mapping is
     * the owning side of the association.
     *
     * @var boolean
     */
77
    public $isOwningSide = true;
romanb's avatar
romanb committed
78 79 80 81 82 83 84
    
    /**
     * Whether the association is optional (0..X) or not (1..X).
     * By default all associations are optional.
     *
     * @var boolean
     */
85
    public $isOptional = true;
romanb's avatar
romanb committed
86 87 88 89 90 91
    
    /**
     * The name of the source Entity (the Entity that defines this mapping).
     *
     * @var string
     */
92
    public $sourceEntityName;
romanb's avatar
romanb committed
93 94 95 96 97 98 99
    
    /**
     * The name of the target Entity (the Enitity that is the target of the
     * association).
     *
     * @var string
     */
100
    public $targetEntityName;
romanb's avatar
romanb committed
101 102 103 104 105 106 107 108
    
    /**
     * Identifies the field on the source class (the class this AssociationMapping
     * belongs to) that represents the association and stores the reference to the
     * other entity/entities.
     *
     * @var string
     */
109
    public $sourceFieldName;
romanb's avatar
romanb committed
110 111 112 113 114 115 116
    
    /**
     * Identifies the field on the owning side that controls the mapping for the
     * association. This is only set on the inverse side of an association.
     *
     * @var string
     */
117
    public $mappedByFieldName;
romanb's avatar
romanb committed
118 119
    
    /**
120
     * The join table definition, if any.
romanb's avatar
romanb committed
121
     *
122
     * @var array
romanb's avatar
romanb committed
123
     */
124
    public $joinTable = array();
125 126

    //protected $_joinTableInsertSql;
romanb's avatar
romanb committed
127 128
    
    /**
129
     * Initializes a new instance of a class derived from AssociationMapping.
romanb's avatar
romanb committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
     *
     * @param array $mapping  The mapping definition.
     */
    public function __construct(array $mapping)
    {
        $this->_validateAndCompleteMapping($mapping);
    }
    
    /**
     * Validates & completes the mapping. Mapping defaults are applied here.
     *
     * @param array $mapping
     */
    protected function _validateAndCompleteMapping(array $mapping)
    {        
        // Mandatory attributes for both sides
        if ( ! isset($mapping['fieldName'])) {
147
            throw MappingException::missingFieldName();
romanb's avatar
romanb committed
148
        }
149
        $this->sourceFieldName = $mapping['fieldName'];
romanb's avatar
romanb committed
150 151
        
        if ( ! isset($mapping['sourceEntity'])) {
152
            throw MappingException::missingSourceEntity($mapping['fieldName']);
romanb's avatar
romanb committed
153
        }
154
        $this->sourceEntityName = $mapping['sourceEntity'];
romanb's avatar
romanb committed
155 156
        
        if ( ! isset($mapping['targetEntity'])) {
157
            throw MappingException::missingTargetEntity($mapping['fieldName']);
romanb's avatar
romanb committed
158
        }
159
        $this->targetEntityName = $mapping['targetEntity'];
romanb's avatar
romanb committed
160 161 162 163 164
        
        // Mandatory and optional attributes for either side
        if ( ! isset($mapping['mappedBy'])) {            
            // Optional
            if (isset($mapping['joinTable'])) {
165
                $this->joinTable = $mapping['joinTable'];   
romanb's avatar
romanb committed
166 167
            }
        } else {
168 169
            $this->isOwningSide = false;
            $this->mappedByFieldName = $mapping['mappedBy'];
romanb's avatar
romanb committed
170 171 172
        }
        
        // Optional attributes for both sides
173
        $this->isOptional = isset($mapping['optional']) ?
romanb's avatar
romanb committed
174
                (bool)$mapping['optional'] : true;
175
        $this->cascades = isset($mapping['cascade']) ?
romanb's avatar
romanb committed
176
                (array)$mapping['cascade'] : array();
177 178 179 180
        $this->isCascadeDelete = in_array('delete', $this->cascades);
        $this->isCascadeSave = in_array('save', $this->cascades);
        $this->isCascadeRefresh = in_array('refresh', $this->cascades);
        $this->isCascadeMerge = in_array('merge', $this->cascades);
romanb's avatar
romanb committed
181 182 183 184 185 186 187 188 189 190
    }
    
    /**
     * Whether the association cascades delete() operations from the source entity
     * to the target entity/entities.
     *
     * @return boolean
     */
    public function isCascadeDelete()
    {
191
        return $this->isCascadeDelete;
romanb's avatar
romanb committed
192 193 194 195 196 197 198 199 200 201
    }
    
    /**
     * Whether the association cascades save() operations from the source entity
     * to the target entity/entities.
     *
     * @return boolean
     */
    public function isCascadeSave()
    {
202
        return $this->isCascadeSave;
romanb's avatar
romanb committed
203 204 205 206 207 208 209 210 211 212
    }
    
    /**
     * Whether the association cascades refresh() operations from the source entity
     * to the target entity/entities.
     *
     * @return boolean
     */
    public function isCascadeRefresh()
    {
213
        return $this->isCascadeRefresh;
romanb's avatar
romanb committed
214
    }
215 216 217 218 219 220 221 222 223

    /**
     * Whether the association cascades merge() operations from the source entity
     * to the target entity/entities.
     *
     * @return boolean
     */
    public function isCascadeMerge()
    {
224
        return $this->isCascadeMerge;
225
    }
romanb's avatar
romanb committed
226 227 228 229 230 231 232 233
    
    /**
     * Whether the target entity/entities of the association are eagerly fetched.
     *
     * @return boolean
     */
    public function isEagerlyFetched()
    {
234
        return $this->fetchMode == self::FETCH_EAGER;
romanb's avatar
romanb committed
235 236 237 238 239 240 241 242 243
    }
    
    /**
     * Whether the target entity/entities of the association are lazily fetched.
     *
     * @return boolean
     */
    public function isLazilyFetched()
    {
244
        return $this->fetchMode == self::FETCH_LAZY;
romanb's avatar
romanb committed
245 246 247 248 249 250 251 252 253
    }
    
    /**
     * Whether the target entity/entities of the association are manually fetched.
     *
     * @return boolean
     */
    public function isManuallyFetched()
    {
254
        return $this->fetchMode == self::FETCH_MANUAL;
romanb's avatar
romanb committed
255 256 257 258 259 260 261 262 263
    }
    
    /**
     * Whether the source entity of this association represents the owning side.
     *
     * @return boolean
     */
    public function isOwningSide()
    {
264
        return $this->isOwningSide;
romanb's avatar
romanb committed
265 266 267 268 269 270 271 272 273
    }
    
    /**
     * Whether the source entity of this association represents the inverse side.
     *
     * @return boolean
     */
    public function isInverseSide()
    {
274
        return ! $this->isOwningSide;
romanb's avatar
romanb committed
275 276 277 278 279 280
    }
    
    /**
     * Whether the association is optional (0..X), or not (1..X).
     *
     * @return boolean TRUE if the association is optional, FALSE otherwise.
281
     * @todo Only applicable to OneToOne. Move there.
romanb's avatar
romanb committed
282 283 284
     */
    public function isOptional()
    {
285
        return $this->isOptional;
romanb's avatar
romanb committed
286 287 288 289 290 291 292 293 294
    }
    
    /**
     * Gets the name of the source entity class.
     *
     * @return string
     */
    public function getSourceEntityName()
    {
295
        return $this->sourceEntityName;
romanb's avatar
romanb committed
296 297 298 299 300 301 302 303 304
    }
    
    /**
     * Gets the name of the target entity class.
     *
     * @return string
     */
    public function getTargetEntityName()
    {
305
        return $this->targetEntityName;
romanb's avatar
romanb committed
306 307 308
    }
    
    /**
309
     * Gets the join table definition, if any.
romanb's avatar
romanb committed
310
     *
311
     * @return array
romanb's avatar
romanb committed
312 313 314
     */
    public function getJoinTable()
    {
315
        return $this->joinTable;
romanb's avatar
romanb committed
316 317 318 319 320 321 322 323 324
    }
    
    /**
     * Get the name of the field the association is mapped into.
     *
     * @return string
     */
    public function getSourceFieldName()
    {
325
        return $this->sourceFieldName;
romanb's avatar
romanb committed
326 327 328 329
    }
    
    /**
     * Gets the field name of the owning side in a bi-directional association.
330 331
     * This is only set on the inverse side. When invoked on the owning side,
     * NULL is returned.
romanb's avatar
romanb committed
332 333 334 335 336
     *
     * @return string
     */
    public function getMappedByFieldName()
    {
337
        return $this->mappedByFieldName;
romanb's avatar
romanb committed
338
    }
339 340 341 342 343 344

    /**
     * Whether the association is a one-to-one association.
     *
     * @return boolean
     */
romanb's avatar
romanb committed
345 346 347 348
    public function isOneToOne()
    {
        return false;
    }
349 350 351 352 353 354

    /**
     * Whether the association is a one-to-many association.
     *
     * @return boolean
     */
romanb's avatar
romanb committed
355 356 357 358
    public function isOneToMany()
    {
        return false;
    }
359 360 361 362 363 364

    /**
     * Whether the association is a many-to-many association.
     *
     * @return boolean
     */
romanb's avatar
romanb committed
365 366 367 368
    public function isManyToMany()
    {
        return false;
    }
369

370 371 372 373 374
    /**
     * Whether the association uses a join table for the mapping.
     *
     * @return boolean
     */
375 376
    public function usesJoinTable()
    {
377
        return (bool)$this->joinTable;
378 379
    }

380 381 382 383 384
    /**
     *
     * @param <type> $entity
     * @param <type> $entityManager 
     */
385
    /*abstract public function load($entity, $em) {}*/
386
}