2008-09-12 13:27:03 +04:00
|
|
|
<?php
|
2009-02-04 19:35:36 +03:00
|
|
|
/*
|
|
|
|
* $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.doctrine-project.org>.
|
|
|
|
*/
|
2008-09-12 13:27:03 +04:00
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
namespace Doctrine\ORM\Mapping;
|
|
|
|
|
2008-09-12 13:27:03 +04:00
|
|
|
/**
|
|
|
|
* A many-to-many mapping describes the mapping between two collections of
|
|
|
|
* entities.
|
|
|
|
*
|
2009-05-14 22:34:12 +04:00
|
|
|
* <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).
|
|
|
|
*
|
2008-09-12 13:27:03 +04:00
|
|
|
* @since 2.0
|
2009-02-04 19:35:36 +03:00
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
2009-07-28 15:43:42 +04:00
|
|
|
* @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
|
2008-09-12 13:27:03 +04:00
|
|
|
*/
|
2009-02-04 19:35:36 +03:00
|
|
|
class ManyToManyMapping extends AssociationMapping
|
|
|
|
{
|
2008-09-12 13:27:03 +04:00
|
|
|
/**
|
2009-02-04 19:35:36 +03:00
|
|
|
* The key columns of the source table.
|
2008-09-12 13:27:03 +04:00
|
|
|
*/
|
2009-05-14 22:34:12 +04:00
|
|
|
public $sourceKeyColumns = array();
|
2008-09-12 13:27:03 +04:00
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
/**
|
|
|
|
* The key columns of the target table.
|
|
|
|
*/
|
2009-05-14 22:34:12 +04:00
|
|
|
public $targetKeyColumns = array();
|
2008-09-12 13:27:03 +04:00
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
/**
|
|
|
|
* Maps the columns in the source table to the columns in the relation table.
|
|
|
|
*/
|
2009-05-14 22:34:12 +04:00
|
|
|
public $sourceToRelationKeyColumns = array();
|
2008-09-12 13:27:03 +04:00
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
/**
|
|
|
|
* Maps the columns in the target table to the columns in the relation table.
|
|
|
|
*/
|
2009-05-14 22:34:12 +04:00
|
|
|
public $targetToRelationKeyColumns = array();
|
2009-02-06 20:16:39 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The columns on the join table.
|
|
|
|
*/
|
2009-05-14 22:34:12 +04:00
|
|
|
public $joinTableColumns = array();
|
2008-09-12 13:27:03 +04:00
|
|
|
|
2009-06-14 21:34:28 +04:00
|
|
|
/** FUTURE: The key column mapping, if any. The key column holds the keys of the Collection. */
|
|
|
|
//public $keyColumn;
|
|
|
|
|
2008-09-12 13:27:03 +04:00
|
|
|
/**
|
2009-02-04 19:35:36 +03:00
|
|
|
* Initializes a new ManyToManyMapping.
|
2008-09-12 13:27:03 +04:00
|
|
|
*
|
2009-02-04 19:35:36 +03:00
|
|
|
* @param array $mapping The mapping information.
|
2008-09-12 13:27:03 +04:00
|
|
|
*/
|
|
|
|
public function __construct(array $mapping)
|
|
|
|
{
|
|
|
|
parent::__construct($mapping);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates and completes the mapping.
|
|
|
|
*
|
|
|
|
* @param array $mapping
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
protected function _validateAndCompleteMapping(array $mapping)
|
|
|
|
{
|
|
|
|
parent::_validateAndCompleteMapping($mapping);
|
|
|
|
if ($this->isOwningSide()) {
|
2009-02-04 19:35:36 +03:00
|
|
|
// owning side MUST have a join table
|
2008-09-12 13:27:03 +04:00
|
|
|
if ( ! isset($mapping['joinTable'])) {
|
2009-02-04 19:35:36 +03:00
|
|
|
throw MappingException::joinTableRequired($mapping['fieldName']);
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
2009-02-06 20:16:39 +03:00
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
// owning side MUST specify joinColumns
|
|
|
|
if ( ! isset($mapping['joinTable']['joinColumns'])) {
|
|
|
|
throw MappingException::invalidMapping($this->_sourceFieldName);
|
|
|
|
}
|
|
|
|
foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) {
|
2009-05-14 22:34:12 +04:00
|
|
|
$this->sourceToRelationKeyColumns[$joinColumn['referencedColumnName']] = $joinColumn['name'];
|
|
|
|
$this->joinTableColumns[] = $joinColumn['name'];
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
2009-05-14 22:34:12 +04:00
|
|
|
$this->sourceKeyColumns = array_keys($this->sourceToRelationKeyColumns);
|
2009-02-06 20:16:39 +03:00
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
// owning side MUST specify inverseJoinColumns
|
|
|
|
if ( ! isset($mapping['joinTable']['inverseJoinColumns'])) {
|
|
|
|
throw MappingException::invalidMapping($this->_sourceFieldName);
|
|
|
|
}
|
|
|
|
foreach ($mapping['joinTable']['inverseJoinColumns'] as $inverseJoinColumn) {
|
2009-05-14 22:34:12 +04:00
|
|
|
$this->targetToRelationKeyColumns[$inverseJoinColumn['referencedColumnName']] = $inverseJoinColumn['name'];
|
|
|
|
$this->joinTableColumns[] = $inverseJoinColumn['name'];
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
2009-05-14 22:34:12 +04:00
|
|
|
$this->targetKeyColumns = array_keys($this->targetToRelationKeyColumns);
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
|
2009-02-06 20:16:39 +03:00
|
|
|
public function getJoinTableColumns()
|
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->joinTableColumns;
|
2009-02-06 20:16:39 +03:00
|
|
|
}
|
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
public function getSourceToRelationKeyColumns()
|
2008-09-12 13:27:03 +04:00
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->sourceToRelationKeyColumns;
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
|
|
|
|
public function getTargetToRelationKeyColumns()
|
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->targetToRelationKeyColumns;
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getSourceKeyColumns()
|
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->sourceKeyColumns;
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getTargetKeyColumns()
|
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->targetKeyColumns;
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
/**
|
|
|
|
* Loads entities in $targetCollection using $em.
|
|
|
|
* The data of $sourceEntity are used to restrict the collection
|
|
|
|
* via the join table.
|
|
|
|
*/
|
|
|
|
public function load($sourceEntity, $targetCollection, $em, array $joinColumnValues = array())
|
2009-02-04 19:35:36 +03:00
|
|
|
{
|
2009-07-28 15:43:42 +04:00
|
|
|
$sourceClass = $em->getClassMetadata($this->sourceEntityName);
|
|
|
|
$joinTableConditions = array();
|
|
|
|
if ($this->isOwningSide()) {
|
|
|
|
$joinTable = $this->getJoinTable();
|
|
|
|
$joinClauses = $this->getTargetToRelationKeyColumns();
|
|
|
|
foreach ($this->getSourceToRelationKeyColumns() as $sourceKeyColumn => $relationKeyColumn) {
|
|
|
|
// getting id
|
|
|
|
if (isset($sourceClass->reflFields[$sourceKeyColumn])) {
|
|
|
|
$joinTableConditions[$relationKeyColumn] = $this->_getPrivateValue($sourceClass, $sourceEntity, $sourceKeyColumn);
|
|
|
|
} else {
|
|
|
|
$joinTableConditions[$relationKeyColumn] = $joinColumnValues[$sourceKeyColumn];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$owningAssoc = $em->getClassMetadata($this->targetEntityName)->getAssociationMapping($this->mappedByFieldName);
|
|
|
|
$joinTable = $owningAssoc->getJoinTable();
|
|
|
|
// TRICKY: since the association is inverted source and target are flipped
|
|
|
|
$joinClauses = $owningAssoc->getSourceToRelationKeyColumns();
|
|
|
|
foreach ($owningAssoc->getTargetToRelationKeyColumns() as $sourceKeyColumn => $relationKeyColumn) {
|
|
|
|
// getting id
|
|
|
|
if (isset($sourceClass->reflFields[$sourceKeyColumn])) {
|
|
|
|
$joinTableConditions[$relationKeyColumn] = $this->_getPrivateValue($sourceClass, $sourceEntity, $sourceKeyColumn);
|
|
|
|
} else {
|
|
|
|
$joinTableConditions[$relationKeyColumn] = $joinColumnValues[$sourceKeyColumn];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$joinTableCriteria = array(
|
|
|
|
'table' => $joinTable['name'],
|
|
|
|
'join' => $joinClauses,
|
|
|
|
'criteria' => $joinTableConditions
|
|
|
|
);
|
|
|
|
$persister = $em->getUnitOfWork()->getEntityPersister($this->targetEntityName);
|
|
|
|
$persister->loadCollection(array($joinTableCriteria), $targetCollection);
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
|
|
|
|
2008-09-12 13:27:03 +04:00
|
|
|
/**
|
2009-02-04 19:35:36 +03:00
|
|
|
* {@inheritdoc}
|
2008-09-12 13:27:03 +04:00
|
|
|
*
|
2009-02-04 19:35:36 +03:00
|
|
|
* @override
|
2008-09-12 13:27:03 +04:00
|
|
|
*/
|
2009-02-04 19:35:36 +03:00
|
|
|
public function isManyToMany()
|
2008-09-12 13:27:03 +04:00
|
|
|
{
|
2009-02-04 19:35:36 +03:00
|
|
|
return true;
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
2009-07-17 17:41:03 +04:00
|
|
|
}
|