2008-09-12 13:27:03 +04:00
|
|
|
<?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
|
2009-05-13 19:19:27 +04:00
|
|
|
* <http://www.doctrine-project.org>.
|
2008-09-12 13:27:03 +04:00
|
|
|
*/
|
|
|
|
|
2009-01-22 22:38:10 +03:00
|
|
|
namespace Doctrine\ORM\Mapping;
|
2008-09-12 13:27:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A one-to-one mapping describes a uni-directional mapping from one entity
|
|
|
|
* to another entity.
|
|
|
|
*
|
2009-05-14 22:34:12 +04:00
|
|
|
* <b>IMPORTANT NOTE:</b>
|
|
|
|
*
|
|
|
|
* The fields of this class are only public for 2 reasons:
|
2010-03-15 20:19:00 +03:00
|
|
|
* 1) To allow fast READ access.
|
2009-05-14 22:34:12 +04:00
|
|
|
* 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).
|
2010-02-09 20:13:49 +03:00
|
|
|
*
|
|
|
|
* Instances of this class are stored serialized in the metadata cache together with the
|
|
|
|
* owning <tt>ClassMetadata</tt> instance.
|
2009-05-14 22:34:12 +04:00
|
|
|
*
|
2008-09-12 13:27:03 +04:00
|
|
|
* @since 2.0
|
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2009-07-20 16:05:19 +04:00
|
|
|
* @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
|
2008-09-12 13:27:03 +04:00
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class OneToOneMapping extends AssociationMapping
|
2008-09-12 13:27:03 +04:00
|
|
|
{
|
|
|
|
/**
|
2010-03-15 20:19:00 +03:00
|
|
|
* READ-ONLY: Maps the source foreign/primary key columns to the target primary/foreign key columns.
|
2008-09-12 13:27:03 +04:00
|
|
|
* i.e. source.id (pk) => target.user_id (fk).
|
|
|
|
* Reverse mapping of _targetToSourceKeyColumns.
|
|
|
|
*/
|
2009-05-14 22:34:12 +04:00
|
|
|
public $sourceToTargetKeyColumns = array();
|
2008-09-12 13:27:03 +04:00
|
|
|
|
|
|
|
/**
|
2010-03-15 20:19:00 +03:00
|
|
|
* READ-ONLY: Maps the target primary/foreign key columns to the source foreign/primary key columns.
|
2008-09-12 13:27:03 +04:00
|
|
|
* i.e. target.user_id (fk) => source.id (pk).
|
|
|
|
* Reverse mapping of _sourceToTargetKeyColumns.
|
|
|
|
*/
|
2009-05-14 22:34:12 +04:00
|
|
|
public $targetToSourceKeyColumns = array();
|
2008-09-12 13:27:03 +04:00
|
|
|
|
|
|
|
/**
|
2010-03-15 20:19:00 +03:00
|
|
|
* READ-ONLY: Whether to delete orphaned elements (when nulled out, i.e. $foo->other = null)
|
2008-09-12 13:27:03 +04:00
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2009-07-23 13:52:16 +04:00
|
|
|
public $orphanRemoval = false;
|
2009-02-04 19:35:36 +03:00
|
|
|
|
|
|
|
/**
|
2010-03-15 20:19:00 +03:00
|
|
|
* READ-ONLY: The join column definitions. Only present on the owning side.
|
2009-02-04 19:35:36 +03:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-05-14 22:34:12 +04:00
|
|
|
public $joinColumns = array();
|
2008-09-12 13:27:03 +04:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
/**
|
2010-03-15 20:19:00 +03:00
|
|
|
* READ-ONLY: A map of join column names to field names that are used in cases
|
2009-08-11 14:51:38 +04:00
|
|
|
* when the join columns are fetched as part of the query result.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2009-07-20 19:30:54 +04:00
|
|
|
public $joinColumnFieldNames = array();
|
2010-03-15 20:19:00 +03:00
|
|
|
|
2008-09-12 13:27:03 +04:00
|
|
|
/**
|
2009-01-06 20:22:23 +03:00
|
|
|
* {@inheritdoc}
|
2008-09-12 13:27:03 +04:00
|
|
|
*
|
|
|
|
* @param array $mapping The mapping to validate & complete.
|
|
|
|
* @return array The validated & completed mapping.
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
protected function _validateAndCompleteMapping(array $mapping)
|
|
|
|
{
|
|
|
|
parent::_validateAndCompleteMapping($mapping);
|
|
|
|
|
2009-07-18 15:41:37 +04:00
|
|
|
if (isset($mapping['joinColumns']) && $mapping['joinColumns']) {
|
|
|
|
$this->isOwningSide = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isOwningSide) {
|
2010-02-09 20:13:49 +03:00
|
|
|
if ( ! isset($mapping['joinColumns']) || ! $mapping['joinColumns']) {
|
|
|
|
// Apply default join column
|
|
|
|
$mapping['joinColumns'] = array(array(
|
|
|
|
'name' => $this->sourceFieldName . '_id',
|
|
|
|
'referencedColumnName' => 'id'
|
|
|
|
));
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
foreach ($mapping['joinColumns'] as $joinColumn) {
|
2009-05-14 22:34:12 +04:00
|
|
|
$this->sourceToTargetKeyColumns[$joinColumn['name']] = $joinColumn['referencedColumnName'];
|
2009-07-20 19:30:54 +04:00
|
|
|
$this->joinColumnFieldNames[$joinColumn['name']] = isset($joinColumn['fieldName'])
|
|
|
|
? $joinColumn['fieldName'] : $joinColumn['name'];
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
2009-08-11 14:51:38 +04:00
|
|
|
$this->joinColumns = $mapping['joinColumns'];
|
2009-05-14 22:34:12 +04:00
|
|
|
$this->targetToSourceKeyColumns = array_flip($this->sourceToTargetKeyColumns);
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
2010-03-15 20:19:00 +03:00
|
|
|
|
|
|
|
//TODO: if orphanRemoval, cascade=remove is implicit!
|
2009-07-23 13:52:16 +04:00
|
|
|
$this->orphanRemoval = isset($mapping['orphanRemoval']) ?
|
|
|
|
(bool) $mapping['orphanRemoval'] : false;
|
2010-03-15 20:19:00 +03:00
|
|
|
|
2008-09-12 13:27:03 +04:00
|
|
|
return $mapping;
|
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
|
2008-09-12 13:27:03 +04:00
|
|
|
/**
|
2009-01-06 20:22:23 +03:00
|
|
|
* {@inheritdoc}
|
2008-09-12 13:27:03 +04:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function isOneToOne()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2010-03-05 19:35:00 +03:00
|
|
|
|
2008-09-12 13:27:03 +04:00
|
|
|
/**
|
2009-01-06 20:22:23 +03:00
|
|
|
* {@inheritdoc}
|
2008-09-12 13:27:03 +04:00
|
|
|
*
|
2009-07-20 16:05:19 +04:00
|
|
|
* @param object $sourceEntity the entity source of this association
|
|
|
|
* @param object $targetEntity the entity to load data in
|
2009-05-13 19:19:27 +04:00
|
|
|
* @param EntityManager $em
|
2009-10-15 00:18:36 +04:00
|
|
|
* @param array $joinColumnValues Values of the join columns of $sourceEntity.
|
2008-09-12 13:27:03 +04:00
|
|
|
*/
|
2009-07-28 15:43:42 +04:00
|
|
|
public function load($sourceEntity, $targetEntity, $em, array $joinColumnValues = array())
|
2008-09-12 13:27:03 +04:00
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
$targetClass = $em->getClassMetadata($this->targetEntityName);
|
2009-05-13 19:19:27 +04:00
|
|
|
|
2009-05-14 22:34:12 +04:00
|
|
|
if ($this->isOwningSide) {
|
2009-11-03 21:30:21 +03:00
|
|
|
$inverseField = isset($targetClass->inverseMappings[$this->sourceEntityName][$this->sourceFieldName]) ?
|
|
|
|
$targetClass->inverseMappings[$this->sourceEntityName][$this->sourceFieldName]->sourceFieldName
|
|
|
|
: false;
|
2009-07-21 13:25:14 +04:00
|
|
|
|
2010-01-30 00:24:29 +03:00
|
|
|
// Mark inverse side as fetched in the hints, otherwise the UoW would
|
|
|
|
// try to load it in a separate query (remember: to-one inverse sides can not be lazy).
|
2009-11-03 21:30:21 +03:00
|
|
|
$hints = array();
|
|
|
|
if ($inverseField) {
|
2010-01-30 00:24:29 +03:00
|
|
|
$hints['fetched'][$targetClass->name][$inverseField] = true;
|
|
|
|
if ($targetClass->subClasses) {
|
|
|
|
foreach ($targetClass->subClasses as $targetSubclassName) {
|
|
|
|
$hints['fetched'][$targetSubclassName][$inverseField] = true;
|
|
|
|
}
|
|
|
|
}
|
2009-11-03 21:30:21 +03:00
|
|
|
}
|
2009-12-16 00:06:32 +03:00
|
|
|
/* cascade read-only status
|
|
|
|
if ($em->getUnitOfWork()->isReadOnly($sourceEntity)) {
|
|
|
|
$hints[Query::HINT_READ_ONLY] = true;
|
|
|
|
}
|
|
|
|
*/
|
2009-11-03 21:30:21 +03:00
|
|
|
|
|
|
|
$targetEntity = $em->getUnitOfWork()->getEntityPersister($this->targetEntityName)->load($joinColumnValues, $targetEntity, $this, $hints);
|
2009-07-21 13:25:14 +04:00
|
|
|
|
2010-01-30 00:24:29 +03:00
|
|
|
if ($targetEntity !== null && $inverseField && ! $targetClass->isCollectionValuedAssociation($inverseField)) {
|
2009-11-03 21:30:21 +03:00
|
|
|
$targetClass->reflFields[$inverseField]->setValue($targetEntity, $sourceEntity);
|
2009-05-13 19:19:27 +04:00
|
|
|
}
|
|
|
|
} else {
|
2009-11-03 21:30:21 +03:00
|
|
|
$conditions = array();
|
|
|
|
$sourceClass = $em->getClassMetadata($this->sourceEntityName);
|
2010-03-15 20:19:00 +03:00
|
|
|
$owningAssoc = $targetClass->getAssociationMapping($this->mappedBy);
|
2009-07-28 15:43:42 +04:00
|
|
|
// TRICKY: since the association is specular source and target are flipped
|
2009-08-11 14:51:38 +04:00
|
|
|
foreach ($owningAssoc->targetToSourceKeyColumns as $sourceKeyColumn => $targetKeyColumn) {
|
2009-10-15 21:07:37 +04:00
|
|
|
if (isset($sourceClass->fieldNames[$sourceKeyColumn])) {
|
2009-08-11 14:51:38 +04:00
|
|
|
$conditions[$targetKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
|
2009-07-20 16:05:19 +04:00
|
|
|
} else {
|
2010-01-30 00:24:29 +03:00
|
|
|
throw MappingException::joinColumnMustPointToMappedField(
|
|
|
|
$sourceClass->name, $sourceKeyColumn
|
|
|
|
);
|
2009-07-20 16:05:19 +04:00
|
|
|
}
|
2009-05-13 19:19:27 +04:00
|
|
|
}
|
2010-03-15 20:19:00 +03:00
|
|
|
|
2009-10-15 18:39:43 +04:00
|
|
|
$targetEntity = $em->getUnitOfWork()->getEntityPersister($this->targetEntityName)->load($conditions, $targetEntity, $this);
|
2009-07-21 13:25:14 +04:00
|
|
|
|
2009-10-28 13:31:47 +03:00
|
|
|
if ($targetEntity !== null) {
|
2010-03-15 20:19:00 +03:00
|
|
|
$targetClass->setFieldValue($targetEntity, $this->mappedBy, $sourceEntity);
|
2009-10-28 13:31:47 +03:00
|
|
|
}
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
2010-03-15 20:19:00 +03:00
|
|
|
|
2009-10-28 13:31:47 +03:00
|
|
|
return $targetEntity;
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
2009-07-17 17:41:03 +04:00
|
|
|
}
|