. */ namespace Doctrine\ORM\Mapping; /** * A one-to-one mapping describes a uni-directional mapping from one entity * to another entity. * * @since 2.0 * @author Roman Borschel */ class OneToOneMapping extends 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. */ private $_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. */ private $_targetToSourceKeyColumns = array(); /** * Whether to delete orphaned elements (when nulled out, i.e. $foo->other = null) * * @var boolean */ private $_deleteOrphans = false; /** * The join column definitions. * * @var array */ private $_joinColumns = array(); /** * Creates a new OneToOneMapping. * * @param array $mapping The mapping info. */ public function __construct(array $mapping) { parent::__construct($mapping); } /** * {@inheritdoc} * * @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'])) { throw MappingException::invalidMapping($this->_sourceFieldName); } $this->_joinColumns = $mapping['joinColumns']; foreach ($mapping['joinColumns'] as $joinColumn) { $this->_sourceToTargetKeyColumns[$joinColumn['name']] = $joinColumn['referencedColumnName']; } $this->_targetToSourceKeyColumns = array_flip($this->_sourceToTargetKeyColumns); } $this->_deleteOrphans = isset($mapping['deleteOrphans']) ? (bool)$mapping['deleteOrphans'] : false; return $mapping; } /** * Gets the join column definitions for this mapping. * * @return array */ public function getJoinColumns() { return $this->_joinColumns; } /** * Gets the source-to-target key column mapping. * * @return array */ public function getSourceToTargetKeyColumns() { return $this->_sourceToTargetKeyColumns; } /** * Gets the target-to-source key column mapping. * * @return array */ public function getTargetToSourceKeyColumns() { return $this->_targetToSourceKeyColumns; } /** * {@inheritdoc} * * @return boolean * @override */ public function isOneToOne() { return true; } /** * {@inheritdoc} * * @param object $owningEntity * @param object $targetEntity * @param EntityManager $em */ public function load($owningEntity, $targetEntity, $em) { $sourceClass = $em->getClassMetadata($this->_sourceEntityName); $targetClass = $em->getClassMetadata($this->_targetEntityName); $conditions = array(); if ($this->_isOwningSide) { foreach ($this->_sourceToTargetKeyColumns as $sourceKeyColumn => $targetKeyColumn) { $conditions[$targetKeyColumn] = $sourceClass->getReflectionProperty( $sourceClass->getFieldName($sourceKeyColumn))->getValue($owningEntity); } if ($targetClass->hasInverseAssociation($this->_sourceFieldName)) { $targetClass->setFieldValue( $targetEntity, $targetClass->inverseMappings[$this->_sourceFieldName]->getSourceFieldName(), $owningEntity); } } else { $owningAssoc = $em->getClassMetadata($this->_targetEntityName)->getAssociationMapping($this->_mappedByFieldName); foreach ($owningAssoc->getTargetToSourceKeyColumns() as $targetKeyColumn => $sourceKeyColumn) { $conditions[$sourceKeyColumn] = $sourceClass->getReflectionProperty( $sourceClass->getFieldName($targetKeyColumn))->getValue($owningEntity); } $targetClass->setFieldValue($targetEntity, $this->_mappedByFieldName, $owningEntity); } $em->getUnitOfWork()->getEntityPersister($this->_targetEntityName)->load($conditions, $targetEntity); } }