2008-09-12 13:27:03 +04:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* 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-02-04 19:35:36 +03: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
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a one-to-many mapping.
|
|
|
|
*
|
|
|
|
* NOTE: One-to-many mappings can currently not be uni-directional (one -> many).
|
|
|
|
* They must either be bidirectional (one <-> many) or unidirectional (many -> one).
|
|
|
|
* In other words, the many-side MUST be the owning side and the one-side MUST be
|
|
|
|
* the inverse side.
|
|
|
|
*
|
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
|
|
|
* @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
|
|
|
* @since 2.0
|
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
class OneToManyMapping extends AssociationMapping
|
2008-09-12 13:27:03 +04:00
|
|
|
{
|
2010-03-15 20:19:00 +03:00
|
|
|
/**
|
|
|
|
* READ-ONLY: Whether to delete orphaned elements (removed from the collection)
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2009-07-23 13:52:16 +04:00
|
|
|
public $orphanRemoval = false;
|
2010-03-15 20:19:00 +03: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;
|
2009-07-28 15:43:42 +04:00
|
|
|
|
2010-02-14 22:38:22 +03:00
|
|
|
/**
|
2010-03-15 20:19:00 +03:00
|
|
|
* READ-ONLY: Order this collection by the given SQL snippet.
|
2010-02-14 22:38:22 +03:00
|
|
|
*/
|
2010-03-05 19:35:00 +03:00
|
|
|
public $orderBy;
|
2010-02-14 22:38:22 +03:00
|
|
|
|
2008-09-12 13:27:03 +04:00
|
|
|
/**
|
2009-01-03 22:50:13 +03:00
|
|
|
* Validates and completes the mapping.
|
2008-09-12 13:27:03 +04:00
|
|
|
*
|
|
|
|
* @param array $mapping The mapping to validate and complete.
|
|
|
|
* @return array The validated and completed mapping.
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
protected function _validateAndCompleteMapping(array $mapping)
|
|
|
|
{
|
|
|
|
parent::_validateAndCompleteMapping($mapping);
|
2009-07-28 15:43:42 +04:00
|
|
|
|
2010-01-30 00:24:29 +03:00
|
|
|
// OneToMany-side MUST be inverse (must have mappedBy)
|
2008-09-12 13:27:03 +04:00
|
|
|
if ( ! isset($mapping['mappedBy'])) {
|
2009-02-05 20:34:44 +03:00
|
|
|
throw MappingException::oneToManyRequiresMappedBy($mapping['fieldName']);
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether orphaned elements (removed from the collection) should be deleted.
|
|
|
|
*
|
|
|
|
* @return boolean TRUE if orphaned elements should be deleted, FALSE otherwise.
|
|
|
|
*/
|
|
|
|
public function shouldDeleteOrphans()
|
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->deleteOrphans;
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-07 20:02:13 +03:00
|
|
|
* {@inheritdoc}
|
2008-09-12 13:27:03 +04:00
|
|
|
*
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
public function isOneToMany()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2009-07-28 20:36:24 +04:00
|
|
|
|
|
|
|
/**
|
2009-10-15 21:07:37 +04:00
|
|
|
* Loads a one-to-many collection.
|
2009-07-28 20:36:24 +04:00
|
|
|
*
|
2009-10-15 21:07:37 +04:00
|
|
|
* @param $sourceEntity The entity that owns the collection.
|
|
|
|
* @param $targetCollection The collection to load/fill.
|
|
|
|
* @param $em The EntityManager to use.
|
|
|
|
* @param $joinColumnValues
|
|
|
|
* @return void
|
2009-07-28 20:36:24 +04:00
|
|
|
*/
|
2009-07-28 15:43:42 +04:00
|
|
|
public function load($sourceEntity, $targetCollection, $em, array $joinColumnValues = array())
|
2009-07-17 17:41:03 +04:00
|
|
|
{
|
2009-07-28 15:43:42 +04:00
|
|
|
$persister = $em->getUnitOfWork()->getEntityPersister($this->targetEntityName);
|
|
|
|
// a one-to-many is always inverse (does not have foreign key)
|
|
|
|
$sourceClass = $em->getClassMetadata($this->sourceEntityName);
|
2010-03-15 20:19:00 +03:00
|
|
|
$owningAssoc = $em->getClassMetadata($this->targetEntityName)->associationMappings[$this->mappedBy];
|
2009-07-28 15:43:42 +04:00
|
|
|
// TRICKY: since the association is specular source and target are flipped
|
2009-10-15 21:07:37 +04:00
|
|
|
foreach ($owningAssoc->targetToSourceKeyColumns as $sourceKeyColumn => $targetKeyColumn) {
|
2009-07-28 15:43:42 +04:00
|
|
|
// getting id
|
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-28 15:43:42 +04:00
|
|
|
} else {
|
|
|
|
$conditions[$targetKeyColumn] = $joinColumnValues[$sourceKeyColumn];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-15 00:21:43 +03:00
|
|
|
$persister->loadOneToManyCollection($this, $conditions, $targetCollection);
|
2009-01-03 22:50:13 +03:00
|
|
|
}
|
2010-04-19 00:47:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines which fields get serialized.
|
|
|
|
*
|
|
|
|
* It is only serialized what is necessary for best unserialization performance.
|
|
|
|
* That means any metadata properties that are not set or empty or simply have
|
|
|
|
* their default value are NOT serialized.
|
|
|
|
*
|
|
|
|
* @return array The names of all the fields that should be serialized.
|
|
|
|
*/
|
|
|
|
public function __sleep()
|
|
|
|
{
|
|
|
|
$serialized = parent::__sleep();
|
|
|
|
if ($this->orderBy) {
|
|
|
|
$serialized[] = 'orderBy';
|
|
|
|
}
|
|
|
|
if ($this->orphanRemoval) {
|
|
|
|
$serialized[] = 'orphanRemoval';
|
|
|
|
}
|
|
|
|
return $serialized;
|
|
|
|
}
|
2009-07-17 17:41:03 +04:00
|
|
|
}
|