OneToOneMapping.php 4.93 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 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
<?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
 * <http://www.phpdoctrine.org>.
 */

#namespace Doctrine::ORM::Mappings;

#use Doctrine::ORM::Entity;

/**
 * A one-to-one mapping describes a uni-directional mapping from one entity 
 * to another entity.
 *
 * @since 2.0
 * @author Roman Borschel <roman@code-factory.org>
 * @todo Rename to OneToOneMapping
 */
class Doctrine_ORM_Mapping_OneToOneMapping extends Doctrine_ORM_Mapping_AssociationMapping
{
    /**
     * 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.
     */
    protected $_sourceToTargetKeyColumns = array();

    /**
     * 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.
     */
    protected $_targetToSourceKeyColumns = array();
    
    /**
     * Whether to delete orphaned elements (when nulled out, i.e. $foo->other = null)
     * 
     * @var boolean
     */
    protected $_deleteOrphans = false;
    
    /**
     * Constructor.
     * Creates a new OneToOneMapping.
     *
     * @param array $mapping  The mapping info.
     */
    public function __construct(array $mapping)
    {
        parent::__construct($mapping);
    }
    
    protected function _initMappingArray()
    {
        parent::_initMappingArray();
        $this->_mapping['deleteOrphans'] = false;
    }
    
    /**
     * Validates & completes the mapping. Mapping defaults are applied here.
     *
     * @param array $mapping  The mapping to validate & complete.
     * @return array  The validated & completed mapping.
     * @override
     */
    protected function _validateAndCompleteMapping(array $mapping)
    {
        parent::_validateAndCompleteMapping($mapping);
        
        if ($this->isOwningSide()) {
            if ( ! isset($mapping['joinColumns'])) {
87
                throw Doctrine_ORM_Exceptions_MappingException::invalidMapping($this->_sourceFieldName);
romanb's avatar
romanb committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
            }
            $this->_sourceToTargetKeyColumns = $mapping['joinColumns'];
            $this->_targetToSourceKeyColumns = array_flip($this->_sourceToTargetKeyColumns);
        }
        
        $this->_deleteOrphans = isset($mapping['deleteOrphans']) ?
                (bool)$mapping['deleteOrphans'] : false;
        
        return $mapping;
    }
    
    /**
     * Gets the source-to-target key column mapping.
     *
     * @return unknown
     */
    public function getSourceToTargetKeyColumns()
    {
        return $this->_sourceToTargetKeyColumns;
    }
    
    /**
     * Gets the target-to-source key column mapping.
     *
     * @return unknown
     */
    public function getTargetToSourceKeyColumns()
    {
        return $this->_targetToSourceKeyColumns;
    }
    
    /**
     * Whether the association is one-to-one.
     *
     * @return boolean
     * @override
     */
    public function isOneToOne()
    {
        return true;
    }
    
    /**
     * Lazy-loads the associated entity for a given entity.
     *
133
     * @param Doctrine\ORM\Entity $entity
romanb's avatar
romanb committed
134 135
     * @return void
     */
136
    public function lazyLoadFor($entity)
romanb's avatar
romanb committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    {
        if ($entity->getClassName() != $this->_sourceClass->getClassName()) {
            //error?
        }
        
        $dql = 'SELECT t.* FROM ' . $this->_targetClass->getClassName() . ' t WHERE ';
        $params = array();
        foreach ($this->_sourceToTargetKeyFields as $sourceKeyField => $targetKeyField) {
            if ($params) {
                $dql .= " AND ";
            }
            $dql .= "t.$targetKeyField = ?";
            $params[] = $entity->_rawGetField($sourceKeyField);
        }
        
        $otherEntity = $this->_targetClass->getEntityManager()
                ->query($dql, $params)
                ->getFirst();
            
        if ( ! $otherEntity) {
            $otherEntity = Doctrine_Null::$INSTANCE;
        }
        $entity->_internalSetReference($this->_sourceFieldName, $otherEntity);
    }
    
}

?>