. */ #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 * @todo Rename to OneToOneMapping */ class Doctrine_Association_OneToOne extends Doctrine_Association { /** * Maps the source foreign/primary key fields to the target primary/foreign key fields. * i.e. source.id (pk) => target.user_id (fk). * Reverse mapping of _targetToSourceKeyFields. */ protected $_sourceToTargetKeyColumns = array(); /** * Maps the target primary/foreign key fields to the source foreign/primary key fields. * i.e. target.user_id (fk) => source.id (pk). * Reverse mapping of _sourceToTargetKeyFields. */ protected $_targetToSourceKeyColumns = array(); /** * Constructor. * Creates a new OneToOneMapping. * * @param array $mapping The mapping info. */ public function __construct(array $mapping) { parent::__construct($mapping); if ($this->isOwningSide()) { $this->_sourceToTargetKeyColumns = $mapping['joinColumns']; $this->_targetToSourceKeyColumns = array_flip($this->_sourceToTargetKeyColumns); } } /** * 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 _validateMapping(array $mapping) { $mapping = parent::_validateMapping($mapping); if ($this->isOwningSide()) { if ( ! isset($mapping['joinColumns'])) { throw Doctrine_MappingException::missingJoinColumns(); } } 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; } /** * Lazy-loads the associated entity for a given entity. * * @param Doctrine::ORM::Entity $entity * @return void */ public function lazyLoadFor(Doctrine_Entity $entity) { 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->_rawSetReference($this->_sourceFieldName, $otherEntity); } } ?>