Commit 4661231c authored by romanb's avatar romanb

Minor updates

parent 96a40f00
......@@ -89,7 +89,7 @@ class Doctrine_Association implements Serializable
* The name of the target Entity (the Enitity that is the target of the
* association).
*
* @var unknown_type
* @var string
*/
protected $_targetEntityName;
......@@ -104,12 +104,21 @@ class Doctrine_Association implements Serializable
/**
* Identifies the field on the owning side that has the mapping for the
* association.
* association. This is only set on the inverse side of an association.
*
* @var string
*/
protected $_mappedByFieldName;
/**
* The name of the join table, if any.
*
* @var string
*/
protected $_joinTable;
//protected $_mapping = array();
/**
* Constructor.
* Creates a new AssociationMapping.
......@@ -118,60 +127,67 @@ class Doctrine_Association implements Serializable
*/
public function __construct(array $mapping)
{
/*$this->_mapping = array(
'fieldName' => null,
'sourceEntity' => null,
'targetEntity' => null,
'mappedBy' => null,
'joinColumns' => null,
'joinTable' => null,
'accessor' => null,
'mutator' => null,
'optional' => true,
'cascade' => array()
);
$this->_mapping = array_merge($this->_mapping, $mapping);*/
$this->_validateAndCompleteMapping($mapping);
$this->_sourceEntityName = $mapping['sourceEntity'];
$this->_targetEntityName = $mapping['targetEntity'];
$this->_sourceFieldName = $mapping['fieldName'];
if ( ! $this->_isOwningSide) {
$this->_mappedByFieldName = $mapping['mappedBy'];
}
}
/**
* Validates & completes the mapping. Mapping defaults are applied here.
*
* @param array $mapping
* @return array The validated & completed mapping.
*/
protected function _validateAndCompleteMapping(array $mapping)
{
if (isset($mapping['mappedBy'])) {
// Inverse side, mapping can be found on the owning side.
$this->_isOwningSide = false;
} else {
// Owning side
}
// Mandatory attributes
// Mandatory attributes for both sides
if ( ! isset($mapping['fieldName'])) {
throw Doctrine_MappingException::missingFieldName();
}
$this->_sourceFieldName = $mapping['fieldName'];
if ( ! isset($mapping['sourceEntity'])) {
throw Doctrine_MappingException::missingSourceEntity($mapping['fieldName']);
}
$this->_sourceEntityName = $mapping['sourceEntity'];
if ( ! isset($mapping['targetEntity'])) {
throw Doctrine_MappingException::missingTargetEntity($mapping['fieldName']);
}
$this->_targetEntityName = $mapping['targetEntity'];
// Optional attributes
$this->_customAccessor = isset($mapping['accessor']) ? $mapping['accessor'] : null;
$this->_customMutator = isset($mapping['mutator']) ? $mapping['mutator'] : null;
$this->_isOptional = isset($mapping['isOptional']) ? (bool)$mapping['isOptional'] : true;
$this->_cascades = isset($mapping['cascade']) ? (array)$mapping['cascade'] : array();
return $mapping;
}
protected function _validateAndCompleteInverseSideMapping()
{
}
protected function _validateAndCompleteOwningSideMapping()
{
// Mandatory and optional attributes for either side
if ( ! isset($mapping['mappedBy'])) {
// Optional
if (isset($mapping['joinTable'])) {
$this->_joinTable = $mapping['joinTable'];
}
} else {
$this->_isOwningSide = false;
$this->_mappedByFieldName = $mapping['mappedBy'];
}
// Optional attributes for both sides
if (isset($mapping['accessor'])) {
$this->_customAccessor = $mapping['accessor'];
}
if (isset($mapping['mutator'])) {
$this->_customMutator = $mapping['mutator'];
}
$this->_isOptional = isset($mapping['optional']) ?
(bool)$mapping['optional'] : true;
$this->_cascades = isset($mapping['cascade']) ?
(array)$mapping['cascade'] : array();
}
/**
......@@ -296,6 +312,16 @@ class Doctrine_Association implements Serializable
return $this->_targetEntityName;
}
/**
* Gets the name of the join table.
*
* @return string
*/
public function getJoinTable()
{
return $this->_joinTable;
}
/**
* Get the name of the field the association is mapped into.
*
......
......@@ -10,27 +10,13 @@
* @todo Rename to ManyToManyMapping
*/
class Doctrine_Association_ManyToMany extends Doctrine_Association
{
/**
* Whether the mapping uses an association class.
*
* @var boolean
*/
private $_usesAssociationClass;
{
/**
* The name of the association class (if an association class is used).
*
* @var string
*/
private $_associationClassName;
/**
* The name of the intermediate table.
*
* @var string
*/
private $_relationTableName;
private $_associationClass;
/** The field in the source table that corresponds to the key in the relation table */
protected $_sourceKeyColumns;
......@@ -44,26 +30,48 @@ class Doctrine_Association_ManyToMany extends Doctrine_Association
/** The field in the intermediate table that corresponds to the key in the target table */
protected $_targetRelationKeyColumns;
/**
* Constructor.
* Creates a new ManyToManyMapping.
*
* @param array $mapping The mapping info.
*/
public function __construct(array $mapping)
{
parent::__construct($mapping);
}
/**
* Whether the mapping uses an association class for the intermediary
* table.
* Validates and completes the mapping.
*
* @return boolean
* @param array $mapping
* @override
*/
public function usesAssociationClass()
protected function _validateAndCompleteMapping(array $mapping)
{
parent::_validateAndCompleteMapping($mapping);
if ($this->isOwningSide()) {
// many-many owning MUST have a join table
if ( ! isset($mapping['joinTable'])) {
throw Doctrine_MappingException::joinTableRequired($mapping['fieldName']);
}
// optional attributes for many-many owning side
$this->_associationClass = isset($mapping['associationClass']) ?
$mapping['associationClass'] : null;
}
}
/**
* Gets the name of the intermediate table.
* Whether the mapping uses an association class for the intermediary
* table.
*
* @return string
* @return boolean
*/
public function getRelationTableName()
public function usesAssociationClass()
{
return $this->_relationTableName;
return $this->_associationClass !== null;
}
/**
......@@ -73,7 +81,7 @@ class Doctrine_Association_ManyToMany extends Doctrine_Association
*/
public function getAssociationClassName()
{
return $this->_associationClassName;
return $this->_associationClass;
}
}
......
......@@ -63,8 +63,6 @@ class Doctrine_Association_OneToMany extends Doctrine_Association
public function __construct(array $mapping)
{
parent::__construct($mapping);
// one side in one-many can currently never be owning side, we may support that later
$this->_isOwningSide = false;
}
/**
......@@ -76,13 +74,15 @@ class Doctrine_Association_OneToMany extends Doctrine_Association
*/
protected function _validateAndCompleteMapping(array $mapping)
{
$mapping = parent::_validateAndCompleteMapping($mapping);
parent::_validateAndCompleteMapping($mapping);
// one-side MUST be inverse (must have mappedBy)
if ( ! isset($mapping['mappedBy'])) {
throw Doctrine_MappingException::oneToManyRequiresMappedBy($mapping['fieldName']);
}
return $mapping;
$this->_deleteOrphans = isset($mapping['deleteOrphans']) ?
(bool)$mapping['deleteOrphans'] : false;
}
/**
......
......@@ -47,7 +47,11 @@ class Doctrine_Association_OneToOne extends Doctrine_Association
*/
protected $_targetToSourceKeyColumns = array();
/** Whether to delete orphaned elements (when nulled out, i.e. $foo->other = null) */
/**
* Whether to delete orphaned elements (when nulled out, i.e. $foo->other = null)
*
* @var boolean
*/
protected $_deleteOrphans = false;
/**
......@@ -59,10 +63,6 @@ class Doctrine_Association_OneToOne extends Doctrine_Association
public function __construct(array $mapping)
{
parent::__construct($mapping);
if ($this->isOwningSide()) {
$this->_sourceToTargetKeyColumns = $mapping['joinColumns'];
$this->_targetToSourceKeyColumns = array_flip($this->_sourceToTargetKeyColumns);
}
}
/**
......@@ -72,16 +72,21 @@ class Doctrine_Association_OneToOne extends Doctrine_Association
* @return array The validated & completed mapping.
* @override
*/
protected function _validateMapping(array $mapping)
protected function _validateAndCompleteMapping(array $mapping)
{
$mapping = parent::_validateMapping($mapping);
parent::_validateAndCompleteMapping($mapping);
if ($this->isOwningSide()) {
if ( ! isset($mapping['joinColumns'])) {
throw Doctrine_MappingException::missingJoinColumns();
throw Doctrine_MappingException::invalidMapping($this->_sourceFieldName);
}
$this->_sourceToTargetKeyColumns = $mapping['joinColumns'];
$this->_targetToSourceKeyColumns = array_flip($this->_sourceToTargetKeyColumns);
}
$this->_deleteOrphans = isset($mapping['deleteOrphans']) ?
(bool)$mapping['deleteOrphans'] : false;
return $mapping;
}
......@@ -145,7 +150,7 @@ class Doctrine_Association_OneToOne extends Doctrine_Association
if ( ! $otherEntity) {
$otherEntity = Doctrine_Null::$INSTANCE;
}
$entity->_rawSetReference($this->_sourceFieldName, $otherEntity);
$entity->_internalSetReference($this->_sourceFieldName, $otherEntity);
}
}
......
......@@ -525,8 +525,11 @@ abstract class Doctrine_Entity implements ArrayAccess, Serializable
*
* @param string $fieldName
* @param mixed $value
* @param boolean $completeBidirectional Whether to complete bidirectional associations
* (creating the back-reference). Should only
* be used by hydration.
*/
final public function _internalSetReference($name, $value)
final public function _internalSetReference($name, $value, $completeBidirectional = false)
{
if ($value === Doctrine_Null::$INSTANCE) {
$this->_references[$name] = $value;
......@@ -546,6 +549,20 @@ abstract class Doctrine_Entity implements ArrayAccess, Serializable
}
$this->_references[$name] = $value;
if ($completeBidirectional && $rel->isOneToOne()) {
//TODO: check if $rel is bidirectional, if yes create the back-reference
if ($rel->isOwningSide()) {
//TODO: how to check if its bidirectional? should be as efficient as possible
/*$targetClass = $this->_em->getClassMetadata($rel->getTargetEntityName());
if (($invAssoc = $targetClass->getInverseAssociation($name)) !== null) {
$value->_internalSetReference($invAssoc->getSourceFieldName(), $this);
}*/
} else {
// for sure bi-directional, as there is no inverse side in unidirectional
$value->_internalSetReference($rel->getMappedByFieldName(), $this);
}
}
}
/**
......
......@@ -74,7 +74,7 @@ class Doctrine_Hydrator_RecordDriver
$relatedClass = $this->_em->getClassMetadata($relation->getTargetEntityName());
$coll = $this->getElementCollection($relatedClass->getClassName());
$coll->setReference($entity, $relation);
$entity->_internalSetReference($name, $coll);
$entity->_internalSetReference($name, $coll, true);
$this->_initializedRelations[$entity->getOid()][$name] = true;
}
}
......@@ -108,7 +108,7 @@ class Doctrine_Hydrator_RecordDriver
public function setRelatedElement(Doctrine_Entity $entity1, $property, $entity2)
{
$entity1->_internalSetReference($property, $entity2);
$entity1->_internalSetReference($property, $entity2, true);
}
public function isIndexKeyInUse(Doctrine_Entity $entity, $assocField, $indexField)
......@@ -151,5 +151,4 @@ class Doctrine_Hydrator_RecordDriver
$this->_initializedRelations = array();
}
}
<?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
* <http://www.phpdoctrine.org>.
*/
#namespace Doctrine::ORM::Exceptions;
/**
* A MappingException indicates that something is wrong with the mapping setup.
......@@ -52,6 +73,21 @@ class Doctrine_MappingException extends Doctrine_Exception
{
return new self("OneToMany mapping on field '$fieldName' requires the 'mappedBy' attribute.");
}
public static function joinTableRequired($fieldName)
{
return new self("The mapping of field '$fieldName' requires an the 'joinTable' attribute.");
}
/**
* Generic exception for invalid mappings.
*
* @param string $fieldName
*/
public static function invalidMapping($fieldName)
{
return new self("The mapping of field '$fieldName' is invalid.");
}
}
?>
\ No newline at end of file
......@@ -22,8 +22,6 @@
/**
* Doctrine_Query_QueryResult
*
* @package Doctrine
* @subpackage Query
* @author Roman Borschel <roman@code-factory.org>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://www.phpdoctrine.org
......@@ -33,17 +31,16 @@
abstract class Doctrine_Query_SqlExecutor_Abstract implements Serializable
{
// [TODO] Remove me later!
public $AST;
//public $AST;
protected $_sqlStatements;
public function __construct(Doctrine_Query_Production $AST)
{
// [TODO] Remove me later!
$this->AST = $AST;
//$this->AST = $AST;
}
/**
* Gets the SQL statements that are executed by the executor.
*
......@@ -54,7 +51,6 @@ abstract class Doctrine_Query_SqlExecutor_Abstract implements Serializable
return $this->_sqlStatements;
}
/**
* Executes all sql statements.
*
......@@ -63,7 +59,6 @@ abstract class Doctrine_Query_SqlExecutor_Abstract implements Serializable
*/
abstract public function execute(Doctrine_Connection $conn, array $params);
/**
* Factory method.
* Creates an appropriate sql executor for the given AST.
......
......@@ -23,8 +23,6 @@
* Executes the SQL statements for bulk DQL DELETE statements on classes in
* Class Table Inheritance (JOINED).
*
* @package Doctrine
* @subpackage Query
* @author Roman Borschel <roman@code-factory.org>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://www.phpdoctrine.org
......@@ -32,9 +30,30 @@
* @version $Revision$
* @todo For a good implementation that uses temporary tables see the Hibernate sources:
* (org.hibernate.hql.ast.exec.MultiTableDeleteExecutor).
* @todo Rename to MultiTableDeleteExecutor
*/
class Doctrine_Query_SqlExecutor_MultiTableDelete extends Doctrine_Query_SqlExecutor_Abstract
{
/**
* Enter description here...
*
* @param Doctrine_Query_Production $AST
*/
public function __construct(Doctrine_Query_Production $AST)
{
// TODO: Inspect the AST, create the necessary SQL queries and store them
// in $this->_sqlStatements
}
/**
* Executes all sql statements.
*
* @param Doctrine_Connection $conn The database connection that is used to execute the queries.
* @param array $params The parameters.
* @override
*/
public function execute(Doctrine_Connection $conn, array $params)
{
//...
}
}
\ No newline at end of file
......@@ -23,8 +23,6 @@
* Executes the SQL statements for bulk DQL UPDATE statements on classes in
* Class Table Inheritance (JOINED).
*
* @package Doctrine
* @subpackage Query
* @author Roman Borschel <roman@code-factory.org>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://www.phpdoctrine.org
......@@ -32,6 +30,7 @@
* @version $Revision$
* @todo For a good implementation that uses temporary tables see the Hibernate sources:
* (org.hibernate.hql.ast.exec.MultiTableUpdateExecutor).
* @todo Rename to MultiTableUpdateExecutor
*/
class Doctrine_Query_SqlExecutor_MultiTableUpdate extends Doctrine_Query_SqlExecutor_Abstract
{
......@@ -41,4 +40,15 @@ class Doctrine_Query_SqlExecutor_MultiTableUpdate extends Doctrine_Query_SqlExec
// in $this->_sqlStatements
}
/**
* Executes all sql statements.
*
* @param Doctrine_Connection $conn The database connection that is used to execute the queries.
* @param array $params The parameters.
* @override
*/
public function execute(Doctrine_Connection $conn, array $params)
{
//...
}
}
\ No newline at end of file
......@@ -22,8 +22,6 @@
/**
* Executor that executes the SQL statement for simple DQL SELECT statements.
*
* @package Doctrine
* @subpackage Abstract
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Roman Borschel <roman@code-factory.org>
* @version $Revision$
......
......@@ -23,13 +23,12 @@
* Executor that executes the SQL statements for DQL DELETE/UPDATE statements on classes
* that are mapped to a single table.
*
* @package Doctrine
* @subpackage SingleTableDeleteUpdate
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Roman Borschel <roman@code-factory.org>
* @version $Revision$
* @link www.phpdoctrine.org
* @since 2.0
* @todo This is exactly the same as SingleSelectExecutor. Unify in SingleStatementExecutor.
*/
class Doctrine_Query_SqlExecutor_SingleTableDeleteUpdate extends Doctrine_Query_SqlExecutor_Abstract
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment