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-02-17 13:54:18 +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
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for association mappings.
|
|
|
|
*
|
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
|
|
|
* @author Roman Borschel <roman@code-factory.org>
|
|
|
|
* @since 2.0
|
|
|
|
*/
|
2009-01-22 22:38:10 +03:00
|
|
|
abstract class AssociationMapping
|
2008-09-12 13:27:03 +04:00
|
|
|
{
|
2009-07-21 19:53:58 +04:00
|
|
|
/**
|
|
|
|
* Specifies that an association is to be fetched when it is first accessed.
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
2008-09-12 13:27:03 +04:00
|
|
|
const FETCH_LAZY = 2;
|
2009-07-21 19:53:58 +04:00
|
|
|
/**
|
|
|
|
* Specifies that an association is to be fetched when the owner of the
|
|
|
|
* association is fetched.
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
2008-09-12 13:27:03 +04:00
|
|
|
const FETCH_EAGER = 3;
|
|
|
|
|
2009-07-25 20:33:29 +04:00
|
|
|
public $isCascadeRemove;
|
|
|
|
public $isCascadePersist;
|
2009-05-14 22:34:12 +04:00
|
|
|
public $isCascadeRefresh;
|
|
|
|
public $isCascadeMerge;
|
2009-07-25 20:52:19 +04:00
|
|
|
public $isCascadeDetach;
|
2008-09-12 13:27:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The fetch mode used for the association.
|
|
|
|
*
|
|
|
|
* @var integer
|
|
|
|
*/
|
2009-10-12 16:54:14 +04:00
|
|
|
public $fetchMode;
|
2008-09-12 13:27:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag that indicates whether the class that defines this mapping is
|
|
|
|
* the owning side of the association.
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2009-05-14 22:34:12 +04:00
|
|
|
public $isOwningSide = true;
|
2008-09-12 13:27:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the source Entity (the Entity that defines this mapping).
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2009-05-14 22:34:12 +04:00
|
|
|
public $sourceEntityName;
|
2008-09-12 13:27:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the target Entity (the Enitity that is the target of the
|
|
|
|
* association).
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2009-05-14 22:34:12 +04:00
|
|
|
public $targetEntityName;
|
2008-09-12 13:27:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Identifies the field on the source class (the class this AssociationMapping
|
|
|
|
* belongs to) that represents the association and stores the reference to the
|
|
|
|
* other entity/entities.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2009-05-14 22:34:12 +04:00
|
|
|
public $sourceFieldName;
|
2008-09-12 13:27:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Identifies the field on the owning side that controls the mapping for the
|
|
|
|
* association. This is only set on the inverse side of an association.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2009-05-14 22:34:12 +04:00
|
|
|
public $mappedByFieldName;
|
2008-09-12 13:27:03 +04:00
|
|
|
|
|
|
|
/**
|
2009-02-04 19:35:36 +03:00
|
|
|
* The join table definition, if any.
|
2008-09-12 13:27:03 +04:00
|
|
|
*
|
2009-02-04 19:35:36 +03:00
|
|
|
* @var array
|
2008-09-12 13:27:03 +04:00
|
|
|
*/
|
2009-05-14 22:34:12 +04:00
|
|
|
public $joinTable = array();
|
2009-02-17 13:54:18 +03:00
|
|
|
|
|
|
|
//protected $_joinTableInsertSql;
|
2008-09-12 13:27:03 +04:00
|
|
|
|
|
|
|
/**
|
2009-01-06 20:22:23 +03:00
|
|
|
* Initializes a new instance of a class derived from AssociationMapping.
|
2008-09-12 13:27:03 +04:00
|
|
|
*
|
|
|
|
* @param array $mapping The mapping definition.
|
|
|
|
*/
|
|
|
|
public function __construct(array $mapping)
|
|
|
|
{
|
|
|
|
$this->_validateAndCompleteMapping($mapping);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates & completes the mapping. Mapping defaults are applied here.
|
|
|
|
*
|
|
|
|
* @param array $mapping
|
|
|
|
*/
|
|
|
|
protected function _validateAndCompleteMapping(array $mapping)
|
|
|
|
{
|
|
|
|
// Mandatory attributes for both sides
|
|
|
|
if ( ! isset($mapping['fieldName'])) {
|
2009-02-04 19:35:36 +03:00
|
|
|
throw MappingException::missingFieldName();
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
2009-05-14 22:34:12 +04:00
|
|
|
$this->sourceFieldName = $mapping['fieldName'];
|
2008-09-12 13:27:03 +04:00
|
|
|
|
|
|
|
if ( ! isset($mapping['sourceEntity'])) {
|
2009-02-04 19:35:36 +03:00
|
|
|
throw MappingException::missingSourceEntity($mapping['fieldName']);
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
2009-05-14 22:34:12 +04:00
|
|
|
$this->sourceEntityName = $mapping['sourceEntity'];
|
2008-09-12 13:27:03 +04:00
|
|
|
|
|
|
|
if ( ! isset($mapping['targetEntity'])) {
|
2009-02-04 19:35:36 +03:00
|
|
|
throw MappingException::missingTargetEntity($mapping['fieldName']);
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
2009-05-14 22:34:12 +04:00
|
|
|
$this->targetEntityName = $mapping['targetEntity'];
|
2008-09-12 13:27:03 +04:00
|
|
|
|
|
|
|
// Mandatory and optional attributes for either side
|
|
|
|
if ( ! isset($mapping['mappedBy'])) {
|
|
|
|
// Optional
|
|
|
|
if (isset($mapping['joinTable'])) {
|
2009-08-11 14:51:38 +04:00
|
|
|
if ($mapping['joinTable']['name'][0] == '`') {
|
|
|
|
$mapping['joinTable']['name'] = trim($mapping['joinTable']['name'], '`');
|
|
|
|
$mapping['joinTable']['quoted'] = true;
|
|
|
|
}
|
2009-05-14 22:34:12 +04:00
|
|
|
$this->joinTable = $mapping['joinTable'];
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
|
|
|
} else {
|
2009-05-14 22:34:12 +04:00
|
|
|
$this->isOwningSide = false;
|
|
|
|
$this->mappedByFieldName = $mapping['mappedBy'];
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Optional attributes for both sides
|
2009-10-12 16:54:14 +04:00
|
|
|
$this->fetchMode = isset($mapping['fetch']) ?
|
|
|
|
$mapping['fetch'] : self::FETCH_LAZY;
|
2009-10-26 01:04:13 +03:00
|
|
|
$cascades = isset($mapping['cascade']) ? $mapping['cascade'] : array();
|
|
|
|
$this->isCascadeRemove = in_array('remove', $cascades);
|
|
|
|
$this->isCascadePersist = in_array('persist', $cascades);
|
|
|
|
$this->isCascadeRefresh = in_array('refresh', $cascades);
|
|
|
|
$this->isCascadeMerge = in_array('merge', $cascades);
|
|
|
|
$this->isCascadeDetach = in_array('detach', $cascades);
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the association cascades delete() operations from the source entity
|
|
|
|
* to the target entity/entities.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2009-07-25 20:33:29 +04:00
|
|
|
public function isCascadeRemove()
|
2008-09-12 13:27:03 +04:00
|
|
|
{
|
2009-07-25 20:33:29 +04:00
|
|
|
return $this->isCascadeRemove;
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the association cascades save() operations from the source entity
|
|
|
|
* to the target entity/entities.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2009-07-25 20:33:29 +04:00
|
|
|
public function isCascadePersist()
|
2008-09-12 13:27:03 +04:00
|
|
|
{
|
2009-07-25 20:33:29 +04:00
|
|
|
return $this->isCascadePersist;
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the association cascades refresh() operations from the source entity
|
|
|
|
* to the target entity/entities.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isCascadeRefresh()
|
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->isCascadeRefresh;
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
2009-05-11 14:43:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the association cascades merge() operations from the source entity
|
|
|
|
* to the target entity/entities.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isCascadeMerge()
|
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->isCascadeMerge;
|
2009-05-11 14:43:27 +04:00
|
|
|
}
|
2008-09-12 13:27:03 +04:00
|
|
|
|
2009-07-25 20:52:19 +04:00
|
|
|
/**
|
|
|
|
* Whether the association cascades detach() operations from the source entity
|
|
|
|
* to the target entity/entities.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isCascadeDetach()
|
|
|
|
{
|
|
|
|
return $this->isCascadeDetach;
|
|
|
|
}
|
|
|
|
|
2008-09-12 13:27:03 +04:00
|
|
|
/**
|
|
|
|
* Whether the target entity/entities of the association are eagerly fetched.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isEagerlyFetched()
|
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->fetchMode == self::FETCH_EAGER;
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the target entity/entities of the association are lazily fetched.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isLazilyFetched()
|
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->fetchMode == self::FETCH_LAZY;
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the source entity of this association represents the owning side.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isOwningSide()
|
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->isOwningSide;
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the source entity of this association represents the inverse side.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isInverseSide()
|
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return ! $this->isOwningSide;
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the name of the source entity class.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSourceEntityName()
|
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->sourceEntityName;
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the name of the target entity class.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTargetEntityName()
|
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->targetEntityName;
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-02-04 19:35:36 +03:00
|
|
|
* Gets the join table definition, if any.
|
2008-09-12 13:27:03 +04:00
|
|
|
*
|
2009-02-04 19:35:36 +03:00
|
|
|
* @return array
|
2008-09-12 13:27:03 +04:00
|
|
|
*/
|
|
|
|
public function getJoinTable()
|
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->joinTable;
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of the field the association is mapped into.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSourceFieldName()
|
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->sourceFieldName;
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the field name of the owning side in a bi-directional association.
|
2009-05-07 20:36:27 +04:00
|
|
|
* This is only set on the inverse side. When invoked on the owning side,
|
|
|
|
* NULL is returned.
|
2008-09-12 13:27:03 +04:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getMappedByFieldName()
|
|
|
|
{
|
2009-05-14 22:34:12 +04:00
|
|
|
return $this->mappedByFieldName;
|
2008-09-12 13:27:03 +04:00
|
|
|
}
|
2009-05-11 14:43:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the association is a one-to-one association.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-09-12 13:27:03 +04:00
|
|
|
public function isOneToOne()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-11 14:43:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the association is a one-to-many association.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-09-12 13:27:03 +04:00
|
|
|
public function isOneToMany()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-05-11 14:43:27 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the association is a many-to-many association.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-09-12 13:27:03 +04:00
|
|
|
public function isManyToMany()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-01-03 22:50:13 +03:00
|
|
|
|
2009-05-11 14:43:27 +04:00
|
|
|
/**
|
|
|
|
* Whether the association uses a join table for the mapping.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2009-01-09 19:25:06 +03:00
|
|
|
public function usesJoinTable()
|
|
|
|
{
|
2009-07-25 20:33:29 +04:00
|
|
|
return (bool) $this->joinTable;
|
2009-01-09 19:25:06 +03:00
|
|
|
}
|
2009-10-26 01:04:13 +03:00
|
|
|
|
|
|
|
public function hasCascades()
|
|
|
|
{
|
|
|
|
return $this->isCascadePersist ||
|
|
|
|
$this->isCascadeRemove ||
|
|
|
|
$this->isCascadeRefresh ||
|
|
|
|
$this->isCascadeMerge ||
|
|
|
|
$this->isCascadeDetach;
|
|
|
|
}
|
2009-01-09 19:25:06 +03:00
|
|
|
|
2009-05-11 14:43:27 +04:00
|
|
|
/**
|
2009-07-28 15:43:42 +04:00
|
|
|
* Loads data in $target domain object using this association.
|
2009-07-20 16:05:19 +04:00
|
|
|
* The data comes from the association navigated from $sourceEntity
|
2009-07-17 17:35:44 +04:00
|
|
|
* using $em.
|
2009-05-11 14:43:27 +04:00
|
|
|
*
|
2009-07-20 16:05:19 +04:00
|
|
|
* @param object $sourceEntity
|
2009-07-28 15:43:42 +04:00
|
|
|
* @param object $target an entity or a collection
|
2009-07-17 17:35:44 +04:00
|
|
|
* @param EntityManager $em
|
2009-07-28 15:43:42 +04:00
|
|
|
* @param array $joinColumnValues foreign keys (significative for this
|
|
|
|
* association) of $sourceEntity, if needed
|
2009-05-11 14:43:27 +04:00
|
|
|
*/
|
2009-07-28 15:43:42 +04:00
|
|
|
abstract public function load($sourceEntity, $target, $em, array $joinColumnValues = array());
|
2009-08-11 14:51:38 +04:00
|
|
|
|
2009-07-28 15:43:42 +04:00
|
|
|
/**
|
2009-08-11 14:51:38 +04:00
|
|
|
*
|
|
|
|
* @param $platform
|
|
|
|
* @return unknown_type
|
2009-07-28 15:43:42 +04:00
|
|
|
*/
|
2009-08-11 14:51:38 +04:00
|
|
|
public function getQuotedJoinTableName($platform)
|
2009-07-28 15:43:42 +04:00
|
|
|
{
|
2009-08-11 14:51:38 +04:00
|
|
|
return isset($this->joinTable['quoted']) ?
|
|
|
|
$platform->quoteIdentifier($this->joinTable['name']) :
|
|
|
|
$this->joinTable['name'];
|
2009-07-28 15:43:42 +04:00
|
|
|
}
|
2009-07-17 17:35:44 +04:00
|
|
|
}
|