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).
|
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
|
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-11-21 21:52:02 +03:00
|
|
|
* Maps the columns in the relational table to the columns in the source table.
|
2008-09-12 13:27:03 +04:00
|
|
|
*/
|
2009-11-21 21:52:02 +03:00
|
|
|
public $relationToSourceKeyColumns = array();
|
2008-09-12 13:27:03 +04:00
|
|
|
|
2009-02-04 19:35:36 +03:00
|
|
|
/**
|
2009-11-21 21:52:02 +03:00
|
|
|
* Maps the columns in the relation table to the columns in the target table.
|
2009-02-04 19:35:36 +03:00
|
|
|
*/
|
2009-11-21 21:52:02 +03:00
|
|
|
public $relationToTargetKeyColumns = array();
|
2009-02-06 20:16:39 +03:00
|
|
|
|
|
|
|
/**
|
2009-08-11 14:51:38 +04:00
|
|
|
* List of aggregated column names on the join table.
|
2009-02-06 20:16:39 +03:00
|
|
|
*/
|
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;
|
2010-02-14 22:38:22 +03:00
|
|
|
|
|
|
|
/**
|
2010-02-16 01:50:35 +03:00
|
|
|
* Order this collection by the given DQL snippet.
|
|
|
|
*
|
|
|
|
* Only simple unqualified field names and ASC|DESC are allowed
|
|
|
|
*
|
|
|
|
* @var array
|
2010-02-14 22:38:22 +03:00
|
|
|
*/
|
|
|
|
public $orderBy = null;
|
2009-06-14 21:34:28 +04:00
|
|
|
|
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-07-28 20:36:24 +04:00
|
|
|
* @param array $mapping The mapping definition.
|
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);
|
2009-07-28 20:36:24 +04:00
|
|
|
if ($this->isOwningSide) {
|
2009-02-04 19:35:36 +03:00
|
|
|
// owning side MUST have a join table
|
2010-02-09 20:13:49 +03:00
|
|
|
if ( ! isset($mapping['joinTable']) || ! $mapping['joinTable']) {
|
|
|
|
// Apply default join table
|
|
|
|
$sourceShortName = substr($this->sourceEntityName, strrpos($this->sourceEntityName, '\\') + 1);
|
|
|
|
$targetShortName = substr($this->targetEntityName, strrpos($this->targetEntityName, '\\') + 1);
|
|
|
|
$mapping['joinTable'] = array(
|
|
|
|
'name' => $sourceShortName .'_' . $targetShortName,
|
|
|
|
'joinColumns' => array(
|
|
|
|
array(
|
|
|
|
'name' => $sourceShortName . '_id',
|
|
|
|
'referencedColumnName' => 'id'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'inverseJoinColumns' => array(
|
|
|
|
array(
|
|
|
|
'name' => $targetShortName . '_id',
|
|
|
|
'referencedColumnName' => 'id'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->joinTable = $mapping['joinTable'];
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
// owning side MUST specify joinColumns
|
2010-02-09 20:13:49 +03:00
|
|
|
else if ( ! isset($mapping['joinTable']['joinColumns'])) {
|
2010-01-14 18:48:42 +03:00
|
|
|
throw MappingException::missingRequiredOption(
|
|
|
|
$this->sourceFieldName, 'joinColumns',
|
|
|
|
'Did you think of case sensitivity / plural s?'
|
|
|
|
);
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
|
|
|
// owning side MUST specify inverseJoinColumns
|
2010-02-09 20:13:49 +03:00
|
|
|
else if ( ! isset($mapping['joinTable']['inverseJoinColumns'])) {
|
2010-01-14 18:48:42 +03:00
|
|
|
throw MappingException::missingRequiredOption(
|
|
|
|
$this->sourceFieldName, 'inverseJoinColumns',
|
|
|
|
'Did you think of case sensitivity / plural s?'
|
|
|
|
);
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
2009-08-11 14:51:38 +04:00
|
|
|
|
|
|
|
foreach ($mapping['joinTable']['joinColumns'] as &$joinColumn) {
|
|
|
|
if ($joinColumn['name'][0] == '`') {
|
|
|
|
$joinColumn['name'] = trim($joinColumn['name'], '`');
|
|
|
|
$joinColumn['quoted'] = true;
|
|
|
|
}
|
2009-11-21 21:52:02 +03:00
|
|
|
$this->relationToSourceKeyColumns[$joinColumn['name']] = $joinColumn['referencedColumnName'];
|
2009-08-11 14:51:38 +04:00
|
|
|
$this->joinTableColumns[] = $joinColumn['name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($mapping['joinTable']['inverseJoinColumns'] as &$inverseJoinColumn) {
|
|
|
|
if ($inverseJoinColumn['name'][0] == '`') {
|
|
|
|
$inverseJoinColumn['name'] = trim($inverseJoinColumn['name'], '`');
|
|
|
|
$inverseJoinColumn['quoted'] = true;
|
|
|
|
}
|
2009-11-21 21:52:02 +03:00
|
|
|
$this->relationToTargetKeyColumns[$inverseJoinColumn['name']] = $inverseJoinColumn['referencedColumnName'];
|
2009-05-14 22:34:12 +04:00
|
|
|
$this->joinTableColumns[] = $inverseJoinColumn['name'];
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
2010-02-14 22:38:22 +03:00
|
|
|
|
|
|
|
if (isset($mapping['orderBy'])) {
|
2010-02-27 00:26:06 +03:00
|
|
|
if (!is_array($mapping['orderBy'])) {
|
|
|
|
throw new \InvalidArgumentException("'orderBy' is expected to be an array, not ".gettype($mapping['orderBy']));
|
2010-02-16 01:50:35 +03:00
|
|
|
}
|
2010-02-27 00:26:06 +03:00
|
|
|
$this->orderBy = $mapping['orderBy'];
|
2010-02-14 22:38:22 +03:00
|
|
|
}
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
2009-02-04 19:35:36 +03:00
|
|
|
|
2009-08-11 14:51:38 +04:00
|
|
|
public function getJoinTableColumnNames()
|
2009-02-06 20:16:39 +03:00
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->joinTableColumns;
|
2009-11-21 21:52:02 +03:00
|
|
|
//return array_merge(array_keys($this->relationToSourceKeyColumns), array_keys($this->relationToTargetKeyColumns));
|
2009-02-06 20:16:39 +03:00
|
|
|
}
|
2009-11-21 21:52:02 +03:00
|
|
|
|
|
|
|
public function getRelationToSourceKeyColumns()
|
2009-02-04 19:35:36 +03:00
|
|
|
{
|
2009-11-21 21:52:02 +03:00
|
|
|
return $this->relationToSourceKeyColumns;
|
2009-02-04 19:35:36 +03:00
|
|
|
}
|
|
|
|
|
2009-11-21 21:52:02 +03:00
|
|
|
public function getRelationToTargetKeyColumns()
|
2009-02-04 19:35:36 +03:00
|
|
|
{
|
2009-11-21 21:52:02 +03:00
|
|
|
return $this->relationToTargetKeyColumns;
|
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.
|
2009-07-28 20:36:24 +04:00
|
|
|
*
|
|
|
|
* @param object The owner of the collection.
|
|
|
|
* @param object The collection to populate.
|
|
|
|
* @param array
|
2009-07-28 15:43:42 +04:00
|
|
|
*/
|
|
|
|
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();
|
2009-08-11 14:51:38 +04:00
|
|
|
if ($this->isOwningSide) {
|
2009-11-21 21:52:02 +03:00
|
|
|
foreach ($this->relationToSourceKeyColumns as $relationKeyColumn => $sourceKeyColumn) {
|
2009-07-28 15:43:42 +04:00
|
|
|
// getting id
|
2009-10-26 16:05:32 +03:00
|
|
|
if (isset($sourceClass->fieldNames[$sourceKeyColumn])) {
|
2009-08-11 14:51:38 +04:00
|
|
|
$joinTableConditions[$relationKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
|
2009-07-28 15:43:42 +04:00
|
|
|
} else {
|
2010-02-09 20:13:49 +03:00
|
|
|
throw MappingException::joinColumnMustPointToMappedField(
|
|
|
|
$sourceClass->name, $sourceKeyColumn
|
|
|
|
);
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2009-07-28 20:36:24 +04:00
|
|
|
$owningAssoc = $em->getClassMetadata($this->targetEntityName)->associationMappings[$this->mappedByFieldName];
|
2009-07-28 15:43:42 +04:00
|
|
|
// TRICKY: since the association is inverted source and target are flipped
|
2009-11-21 21:52:02 +03:00
|
|
|
foreach ($owningAssoc->relationToTargetKeyColumns as $relationKeyColumn => $sourceKeyColumn) {
|
2009-07-28 15:43:42 +04:00
|
|
|
// getting id
|
2009-10-26 16:05:32 +03:00
|
|
|
if (isset($sourceClass->fieldNames[$sourceKeyColumn])) {
|
2009-08-11 14:51:38 +04:00
|
|
|
$joinTableConditions[$relationKeyColumn] = $sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
|
2009-07-28 15:43:42 +04:00
|
|
|
} else {
|
2010-02-09 20:13:49 +03:00
|
|
|
throw MappingException::joinColumnMustPointToMappedField(
|
|
|
|
$sourceClass->name, $sourceKeyColumn
|
|
|
|
);
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-11 14:51:38 +04:00
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
$persister = $em->getUnitOfWork()->getEntityPersister($this->targetEntityName);
|
2009-08-11 14:51:38 +04:00
|
|
|
$persister->loadManyToManyCollection($this, $joinTableConditions, $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
|
|
|
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-08-11 14:51:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the (possibly quoted) column name of a join column that is safe to use
|
|
|
|
* in an SQL statement.
|
|
|
|
*
|
|
|
|
* @param string $joinColumn
|
|
|
|
* @param AbstractPlatform $platform
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getQuotedJoinColumnName($joinColumn, $platform)
|
|
|
|
{
|
|
|
|
return isset($this->joinTable['joinColumns'][$joinColumn]['quoted']) ?
|
|
|
|
$platform->quoteIdentifier($joinColumn) :
|
|
|
|
$joinColumn;
|
|
|
|
}
|
2009-07-17 17:41:03 +04:00
|
|
|
}
|