Commit 62446f0f authored by romanb's avatar romanb

[2.0] Cleanup for changeset #6172.

parent 82c41668
...@@ -37,7 +37,7 @@ class ObjectHydrator extends AbstractHydrator ...@@ -37,7 +37,7 @@ class ObjectHydrator extends AbstractHydrator
/* /*
* These two properties maintain their values between hydration runs. * These two properties maintain their values between hydration runs.
*/ */
/* Class entries */ /* Local ClassMetadata cache to avoid going to the EntityManager all the time. */
private $_ce = array(); private $_ce = array();
private $_discriminatorMap = array(); private $_discriminatorMap = array();
/* /*
...@@ -48,9 +48,9 @@ class ObjectHydrator extends AbstractHydrator ...@@ -48,9 +48,9 @@ class ObjectHydrator extends AbstractHydrator
private $_identifierMap = array(); private $_identifierMap = array();
private $_resultPointers = array(); private $_resultPointers = array();
private $_idTemplate = array(); private $_idTemplate = array();
private $_resultCounter = 0; private $_resultCounter;
private $_rootAliases = array(); private $_rootAliases = array();
private $_fetchedAssociations = array(); private $_fetchedAssociations;
/* TODO: Consider unifying _collections and _initializedRelations */ /* TODO: Consider unifying _collections and _initializedRelations */
/** Collections initialized by the hydrator */ /** Collections initialized by the hydrator */
private $_collections = array(); private $_collections = array();
...@@ -63,11 +63,12 @@ class ObjectHydrator extends AbstractHydrator ...@@ -63,11 +63,12 @@ class ObjectHydrator extends AbstractHydrator
$this->_isSimpleQuery = count($this->_rsm->aliasMap) <= 1; $this->_isSimpleQuery = count($this->_rsm->aliasMap) <= 1;
$this->_allowPartialObjects = $this->_em->getConfiguration()->getAllowPartialObjects() $this->_allowPartialObjects = $this->_em->getConfiguration()->getAllowPartialObjects()
|| isset($this->_hints[Query::HINT_FORCE_PARTIAL_LOAD]); || isset($this->_hints[Query::HINT_FORCE_PARTIAL_LOAD]);
$this->_identifierMap = array();
$this->_resultPointers = array(); $this->_identifierMap =
$this->_idTemplate = array(); $this->_resultPointers =
$this->_resultCounter = 0; $this->_idTemplate =
$this->_fetchedAssociations = array(); $this->_fetchedAssociations = array();
$this->_resultCounter = 0;
foreach ($this->_rsm->aliasMap as $dqlAlias => $className) { foreach ($this->_rsm->aliasMap as $dqlAlias => $className) {
$this->_identifierMap[$dqlAlias] = array(); $this->_identifierMap[$dqlAlias] = array();
...@@ -172,25 +173,26 @@ class ObjectHydrator extends AbstractHydrator ...@@ -172,25 +173,26 @@ class ObjectHydrator extends AbstractHydrator
* *
* @param object $entity The entity to which the collection belongs. * @param object $entity The entity to which the collection belongs.
* @param string $name The name of the field on the entity that holds the collection. * @param string $name The name of the field on the entity that holds the collection.
* @return PersistentCollection
*/ */
private function initRelatedCollection($entity, $name) private function initRelatedCollection($entity, $name)
{ {
$oid = spl_object_hash($entity); $oid = spl_object_hash($entity);
$classMetadata = $this->_ce[get_class($entity)]; $class = $this->_ce[get_class($entity)];
$relation = $classMetadata->associationMappings[$name]; $relation = $class->associationMappings[$name];
if ( ! isset($this->_ce[$relation->targetEntityName])) {
$this->_ce[$relation->targetEntityName] = $this->_em->getClassMetadata($relation->targetEntityName);
}
$coll = new PersistentCollection($this->_em, $this->_ce[$relation->targetEntityName]);
$this->_collections[] = $coll;
$coll->setOwner($entity, $relation);
$classMetadata->reflFields[$name]->setValue($entity, $coll); //$coll = $class->reflFields[$name]->getValue($entity);
$this->_uow->setOriginalEntityProperty($oid, $name, $coll); //if ( ! $coll) {
// $coll = new Collection;
//}
$pColl = new PersistentCollection($this->_em, $this->_getClassMetadata($relation->targetEntityName));
$this->_collections[] = $pColl;
$pColl->setOwner($entity, $relation);
$class->reflFields[$name]->setValue($entity, $pColl);
$this->_uow->setOriginalEntityProperty($oid, $name, $pColl);
$this->_initializedRelations[$oid][$name] = true; $this->_initializedRelations[$oid][$name] = true;
return $coll;
} }
/** /**
...@@ -210,10 +212,10 @@ class ObjectHydrator extends AbstractHydrator ...@@ -210,10 +212,10 @@ class ObjectHydrator extends AbstractHydrator
} }
/** /**
* Gets the last key of a collection/array.
* *
* @param <type> $coll * @param Collection|array $coll
* @return <type> * @return string|integer
* @todo Consider inlining this method, introducing $coll->lastKey().
*/ */
private function getLastKey($coll) private function getLastKey($coll)
{ {
...@@ -248,6 +250,7 @@ class ObjectHydrator extends AbstractHydrator ...@@ -248,6 +250,7 @@ class ObjectHydrator extends AbstractHydrator
// Properly initialize any unfetched associations, if partial objects are not allowed. // Properly initialize any unfetched associations, if partial objects are not allowed.
if ( ! $this->_allowPartialObjects) { if ( ! $this->_allowPartialObjects) {
foreach ($this->_ce[$className]->associationMappings as $field => $assoc) { foreach ($this->_ce[$className]->associationMappings as $field => $assoc) {
// Check if the association is not among the fetch-joined associatons already.
if ( ! isset($this->_fetchedAssociations[$className][$field])) { if ( ! isset($this->_fetchedAssociations[$className][$field])) {
if ($assoc->isOneToOne()) { if ($assoc->isOneToOne()) {
$joinColumns = array(); $joinColumns = array();
...@@ -257,7 +260,8 @@ class ObjectHydrator extends AbstractHydrator ...@@ -257,7 +260,8 @@ class ObjectHydrator extends AbstractHydrator
if ($assoc->isLazilyFetched()) { if ($assoc->isLazilyFetched()) {
// Inject proxy // Inject proxy
$this->_ce[$className]->reflFields[$field]->setValue($entity, $this->_ce[$className]->reflFields[$field]->setValue($entity,
$this->_em->getProxyFactory()->getAssociationProxy($entity, $assoc, $joinColumns)); $this->_em->getProxyFactory()->getAssociationProxy($entity, $assoc, $joinColumns)
);
} else { } else {
// Eager load // Eager load
//TODO: Allow more efficient and configurable batching of these loads //TODO: Allow more efficient and configurable batching of these loads
...@@ -265,12 +269,14 @@ class ObjectHydrator extends AbstractHydrator ...@@ -265,12 +269,14 @@ class ObjectHydrator extends AbstractHydrator
} }
} else { } else {
// Inject collection // Inject collection
$collection = $this->initRelatedCollection($entity, $field); $pColl = new PersistentCollection($this->_em, $this->_getClassMetadata($assoc->targetEntityName));
if ($assoc->isLazilyFetched()) { $pColl->setOwner($entity, $assoc);
$collection->setLazyInitialization(); $this->_ce[$className]->reflFields[$field]->setValue($entity, $pColl);
} else { if ( ! $assoc->isLazilyFetched()) {
//TODO: Allow more efficient and configurable batching of these loads //TODO: Allow more efficient and configurable batching of these loads
$assoc->load($entity, $collection, $this->_em); $assoc->load($entity, $pColl, $this->_em);
} else {
$pColl->setInitialized(false);
} }
} }
} }
...@@ -280,6 +286,22 @@ class ObjectHydrator extends AbstractHydrator ...@@ -280,6 +286,22 @@ class ObjectHydrator extends AbstractHydrator
return $entity; return $entity;
} }
/**
* Gets a ClassMetadata instance from the local cache.
* If the instance is not yet in the local cache, it is loaded into the
* local cache.
*
* @param string $className The name of the class.
* @return ClassMetadata
*/
private function _getClassMetadata($className)
{
if ( ! isset($this->_ce[$className])) {
$this->_ce[$className] = $this->_em->getClassMetadata($className);
}
return $this->_ce[$className];
}
/** /**
* Sets a related element. * Sets a related element.
* *
...@@ -463,7 +485,6 @@ class ObjectHydrator extends AbstractHydrator ...@@ -463,7 +485,6 @@ class ObjectHydrator extends AbstractHydrator
$index = $this->_identifierMap[$dqlAlias][$id[$dqlAlias]]; $index = $this->_identifierMap[$dqlAlias][$id[$dqlAlias]];
} }
$this->updateResultPointer($result, $index, $dqlAlias); $this->updateResultPointer($result, $index, $dqlAlias);
//unset($rowData[$dqlAlias]);
} }
} }
......
...@@ -412,7 +412,6 @@ abstract class AssociationMapping ...@@ -412,7 +412,6 @@ abstract class AssociationMapping
*/ */
protected function _getPrivateValue(ClassMetadata $class, $entity, $column) protected function _getPrivateValue(ClassMetadata $class, $entity, $column)
{ {
$reflField = $class->getReflectionProperty($class->getFieldName($column)); return $class->reflFields[$class->fieldNames[$column]]->getValue($entity);
return $reflField->getValue($entity);
} }
} }
...@@ -405,10 +405,10 @@ final class ClassMetadata ...@@ -405,10 +405,10 @@ final class ClassMetadata
public function __construct($entityName) public function __construct($entityName)
{ {
$this->name = $entityName; $this->name = $entityName;
$this->namespace = substr($entityName, 0, strrpos($entityName, '\\'));
$this->primaryTable['name'] = str_replace($this->namespace . '\\', '', $this->name);
$this->rootEntityName = $entityName;
$this->reflClass = new \ReflectionClass($entityName); $this->reflClass = new \ReflectionClass($entityName);
$this->namespace = $this->reflClass->getNamespaceName();
$this->primaryTable['name'] = $this->reflClass->getShortName();
$this->rootEntityName = $entityName;
} }
/** /**
......
...@@ -70,7 +70,7 @@ class ManyToManyMapping extends AssociationMapping ...@@ -70,7 +70,7 @@ class ManyToManyMapping extends AssociationMapping
/** /**
* Initializes a new ManyToManyMapping. * Initializes a new ManyToManyMapping.
* *
* @param array $mapping The mapping information. * @param array $mapping The mapping definition.
*/ */
public function __construct(array $mapping) public function __construct(array $mapping)
{ {
...@@ -86,7 +86,7 @@ class ManyToManyMapping extends AssociationMapping ...@@ -86,7 +86,7 @@ class ManyToManyMapping extends AssociationMapping
protected function _validateAndCompleteMapping(array $mapping) protected function _validateAndCompleteMapping(array $mapping)
{ {
parent::_validateAndCompleteMapping($mapping); parent::_validateAndCompleteMapping($mapping);
if ($this->isOwningSide()) { if ($this->isOwningSide) {
// owning side MUST have a join table // owning side MUST have a join table
if ( ! isset($mapping['joinTable'])) { if ( ! isset($mapping['joinTable'])) {
throw MappingException::joinTableRequired($mapping['fieldName']); throw MappingException::joinTableRequired($mapping['fieldName']);
...@@ -143,15 +143,19 @@ class ManyToManyMapping extends AssociationMapping ...@@ -143,15 +143,19 @@ class ManyToManyMapping extends AssociationMapping
* Loads entities in $targetCollection using $em. * Loads entities in $targetCollection using $em.
* The data of $sourceEntity are used to restrict the collection * The data of $sourceEntity are used to restrict the collection
* via the join table. * via the join table.
*
* @param object The owner of the collection.
* @param object The collection to populate.
* @param array
*/ */
public function load($sourceEntity, $targetCollection, $em, array $joinColumnValues = array()) public function load($sourceEntity, $targetCollection, $em, array $joinColumnValues = array())
{ {
$sourceClass = $em->getClassMetadata($this->sourceEntityName); $sourceClass = $em->getClassMetadata($this->sourceEntityName);
$joinTableConditions = array(); $joinTableConditions = array();
if ($this->isOwningSide()) { if ($this->isOwningSide()) {
$joinTable = $this->getJoinTable(); $joinTable = $this->joinTable;
$joinClauses = $this->getTargetToRelationKeyColumns(); $joinClauses = $this->targetToRelationKeyColumns;
foreach ($this->getSourceToRelationKeyColumns() as $sourceKeyColumn => $relationKeyColumn) { foreach ($this->sourceToRelationKeyColumns as $sourceKeyColumn => $relationKeyColumn) {
// getting id // getting id
if (isset($sourceClass->reflFields[$sourceKeyColumn])) { if (isset($sourceClass->reflFields[$sourceKeyColumn])) {
$joinTableConditions[$relationKeyColumn] = $this->_getPrivateValue($sourceClass, $sourceEntity, $sourceKeyColumn); $joinTableConditions[$relationKeyColumn] = $this->_getPrivateValue($sourceClass, $sourceEntity, $sourceKeyColumn);
...@@ -160,11 +164,11 @@ class ManyToManyMapping extends AssociationMapping ...@@ -160,11 +164,11 @@ class ManyToManyMapping extends AssociationMapping
} }
} }
} else { } else {
$owningAssoc = $em->getClassMetadata($this->targetEntityName)->getAssociationMapping($this->mappedByFieldName); $owningAssoc = $em->getClassMetadata($this->targetEntityName)->associationMappings[$this->mappedByFieldName];
$joinTable = $owningAssoc->getJoinTable(); $joinTable = $owningAssoc->joinTable;
// TRICKY: since the association is inverted source and target are flipped // TRICKY: since the association is inverted source and target are flipped
$joinClauses = $owningAssoc->getSourceToRelationKeyColumns(); $joinClauses = $owningAssoc->sourceToRelationKeyColumns;
foreach ($owningAssoc->getTargetToRelationKeyColumns() as $sourceKeyColumn => $relationKeyColumn) { foreach ($owningAssoc->targetToRelationKeyColumns as $sourceKeyColumn => $relationKeyColumn) {
// getting id // getting id
if (isset($sourceClass->reflFields[$sourceKeyColumn])) { if (isset($sourceClass->reflFields[$sourceKeyColumn])) {
$joinTableConditions[$relationKeyColumn] = $this->_getPrivateValue($sourceClass, $sourceEntity, $sourceKeyColumn); $joinTableConditions[$relationKeyColumn] = $this->_getPrivateValue($sourceClass, $sourceEntity, $sourceKeyColumn);
...@@ -179,13 +183,11 @@ class ManyToManyMapping extends AssociationMapping ...@@ -179,13 +183,11 @@ class ManyToManyMapping extends AssociationMapping
'criteria' => $joinTableConditions 'criteria' => $joinTableConditions
); );
$persister = $em->getUnitOfWork()->getEntityPersister($this->targetEntityName); $persister = $em->getUnitOfWork()->getEntityPersister($this->targetEntityName);
$persister->loadCollection(array($joinTableCriteria), $targetCollection); $persister->loadManyToManyCollection(array($joinTableCriteria), $targetCollection);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @override
*/ */
public function isManyToMany() public function isManyToMany()
{ {
......
...@@ -105,12 +105,21 @@ class OneToManyMapping extends AssociationMapping ...@@ -105,12 +105,21 @@ class OneToManyMapping extends AssociationMapping
return true; return true;
} }
/**
*
*
* @param $sourceEntity
* @param $targetCollection
* @param $em
* @param $joinColumnValues
* @return unknown_type
*/
public function load($sourceEntity, $targetCollection, $em, array $joinColumnValues = array()) public function load($sourceEntity, $targetCollection, $em, array $joinColumnValues = array())
{ {
$persister = $em->getUnitOfWork()->getEntityPersister($this->targetEntityName); $persister = $em->getUnitOfWork()->getEntityPersister($this->targetEntityName);
// a one-to-many is always inverse (does not have foreign key) // a one-to-many is always inverse (does not have foreign key)
$sourceClass = $em->getClassMetadata($this->sourceEntityName); $sourceClass = $em->getClassMetadata($this->sourceEntityName);
$owningAssoc = $em->getClassMetadata($this->targetEntityName)->getAssociationMapping($this->mappedByFieldName); $owningAssoc = $em->getClassMetadata($this->targetEntityName)->associationMappings[$this->mappedByFieldName];
// TRICKY: since the association is specular source and target are flipped // TRICKY: since the association is specular source and target are flipped
foreach ($owningAssoc->getTargetToSourceKeyColumns() as $sourceKeyColumn => $targetKeyColumn) { foreach ($owningAssoc->getTargetToSourceKeyColumns() as $sourceKeyColumn => $targetKeyColumn) {
// getting id // getting id
...@@ -121,6 +130,6 @@ class OneToManyMapping extends AssociationMapping ...@@ -121,6 +130,6 @@ class OneToManyMapping extends AssociationMapping
} }
} }
$persister->loadCollection($conditions, $targetCollection); $persister->loadOneToManyCollection($conditions, $targetCollection);
} }
} }
This diff is collapsed.
...@@ -273,7 +273,7 @@ class JoinedSubclassPersister extends StandardEntityPersister ...@@ -273,7 +273,7 @@ class JoinedSubclassPersister extends StandardEntityPersister
* @todo Quote identifier. * @todo Quote identifier.
* @override * @override
*/ */
protected function _getSelectSingleEntitySql(array &$criteria) protected function _getSelectEntitiesSql(array &$criteria)
{ {
$tableAliases = array(); $tableAliases = array();
$aliasIndex = 1; $aliasIndex = 1;
......
...@@ -395,16 +395,20 @@ class StandardEntityPersister ...@@ -395,16 +395,20 @@ class StandardEntityPersister
*/ */
public function load(array $criteria, $entity = null) public function load(array $criteria, $entity = null)
{ {
$stmt = $this->_conn->prepare($this->_getSelectSingleEntitySql($criteria)); $stmt = $this->_conn->prepare($this->_getSelectEntitiesSql($criteria));
$stmt->execute(array_values($criteria)); $stmt->execute(array_values($criteria));
$result = $stmt->fetch(Connection::FETCH_ASSOC); $result = $stmt->fetch(Connection::FETCH_ASSOC);
$stmt->closeCursor(); $stmt->closeCursor();
return $this->_createEntity($result, $entity); return $this->_createEntity($result, $entity);
} }
public function loadCollection(array $criteria, PersistentCollection $collection) /**
*
*
*/
public function loadOneToManyCollection(array $criteria, PersistentCollection $collection)
{ {
$sql = $this->_getSelectSingleEntitySql($criteria); $sql = $this->_getSelectEntitiesSql($criteria);
$stmt = $this->_conn->prepare($sql); $stmt = $this->_conn->prepare($sql);
$stmt->execute(array_values($criteria)); $stmt->execute(array_values($criteria));
while ($result = $stmt->fetch(Connection::FETCH_ASSOC)) { while ($result = $stmt->fetch(Connection::FETCH_ASSOC)) {
...@@ -413,6 +417,27 @@ class StandardEntityPersister ...@@ -413,6 +417,27 @@ class StandardEntityPersister
$stmt->closeCursor(); $stmt->closeCursor();
} }
/**
*
*
*/
public function loadManyToManyCollection(array $criteria, PersistentCollection $collection)
{
$sql = $this->_getSelectManyToManyEntityCollectionSql($criteria);
$stmt = $this->_conn->prepare($sql);
$stmt->execute(array_values($criteria));
while ($result = $stmt->fetch(Connection::FETCH_ASSOC)) {
$collection->add($this->_createEntity($result));
}
$stmt->closeCursor();
}
/**
*
* @param $result
* @param $entity
* @return object
*/
private function _createEntity($result, $entity = null) private function _createEntity($result, $entity = null)
{ {
$data = array(); $data = array();
...@@ -469,7 +494,7 @@ class StandardEntityPersister ...@@ -469,7 +494,7 @@ class StandardEntityPersister
$coll->setOwner($entity, $assoc); $coll->setOwner($entity, $assoc);
$this->_class->reflFields[$field]->setValue($entity, $coll); $this->_class->reflFields[$field]->setValue($entity, $coll);
if ($assoc->isLazilyFetched()) { if ($assoc->isLazilyFetched()) {
$coll->setLazyInitialization(); $coll->setInitialized(false);
} else { } else {
//TODO: Allow more efficient and configurable batching of these loads //TODO: Allow more efficient and configurable batching of these loads
$assoc->load($entity, $coll, $this->_em); $assoc->load($entity, $coll, $this->_em);
...@@ -490,7 +515,7 @@ class StandardEntityPersister ...@@ -490,7 +515,7 @@ class StandardEntityPersister
* JoinClauses are used to restrict the result returned but only columns of this entity are selected (@see _getJoinSql()). * JoinClauses are used to restrict the result returned but only columns of this entity are selected (@see _getJoinSql()).
* @return string The SQL. * @return string The SQL.
*/ */
protected function _getSelectSingleEntitySql(array &$criteria) protected function _getSelectEntitiesSql(array &$criteria)
{ {
$columnList = ''; $columnList = '';
foreach ($this->_class->columnNames as $column) { foreach ($this->_class->columnNames as $column) {
...@@ -527,6 +552,7 @@ class StandardEntityPersister ...@@ -527,6 +552,7 @@ class StandardEntityPersister
unset($criteria[$field]); unset($criteria[$field]);
continue; continue;
} }
if (isset($this->_class->columnNames[$field])) { if (isset($this->_class->columnNames[$field])) {
$columnName = $this->_class->columnNames[$field]; $columnName = $this->_class->columnNames[$field];
} else if (in_array($field, $joinColumnNames)) { } else if (in_array($field, $joinColumnNames)) {
...@@ -544,7 +570,52 @@ class StandardEntityPersister ...@@ -544,7 +570,52 @@ class StandardEntityPersister
} }
/** /**
* Builds a LEFT JOIN sql query part using $joinClause * Gets the SQL to select a collection of entities in a many-many association.
*
* @return string
*/
protected function _getSelectManyToManyEntityCollectionSql(array &$criteria)
{
$columnList = '';
foreach ($this->_class->columnNames as $column) {
if ($columnList != '') $columnList .= ', ';
$columnList .= $this->_conn->quoteIdentifier($column);
}
if ( ! $this->_em->getConfiguration()->getAllowPartialObjects()) {
foreach ($this->_class->associationMappings as $assoc) {
if ($assoc->isOwningSide && $assoc->isOneToOne()) {
foreach ($assoc->targetToSourceKeyColumns as $srcColumn) {
$columnList .= ', ' . $this->_conn->quoteIdentifier($srcColumn);
}
}
}
}
$joinSql = '';
$conditionSql = '';
foreach ($criteria as $field => $value) {
if ($conditionSql != '') {
$conditionSql .= ' AND ';
}
$joinSql .= $this->_getJoinSql($value);
foreach ($value['criteria'] as $nestedField => $nestedValue) {
$columnName = "{$value['table']}.{$nestedField}";
$conditionSql .= $this->_conn->quoteIdentifier($columnName) . ' = ?';
$criteria[$columnName] = $nestedValue;
}
unset($criteria[$field]);
}
return 'SELECT ' . $columnList
. ' FROM ' . $this->_class->primaryTable['name']
. $joinSql
. ' WHERE ' . $conditionSql;
}
/**
* Builds an INNER JOIN sql query part using $joinClause.
*
* @param array $joinClause keys are: * @param array $joinClause keys are:
* 'table' => table to join * 'table' => table to join
* 'join' => array of [sourceField => joinTableField] * 'join' => array of [sourceField => joinTableField]
...@@ -558,6 +629,6 @@ class StandardEntityPersister ...@@ -558,6 +629,6 @@ class StandardEntityPersister
$clauses[] = $this->_class->getTableName() . ".{$sourceField} = " $clauses[] = $this->_class->getTableName() . ".{$sourceField} = "
. "{$joinClause['table']}.{$joinTableField}"; . "{$joinClause['table']}.{$joinTableField}";
} }
return " LEFT JOIN {$joinClause['table']} ON " . implode(' AND ', $clauses); return ' INNER JOIN ' . $joinClause['table'] . ' ON ' . implode(' AND ', $clauses);
} }
} }
...@@ -248,11 +248,8 @@ class SqlWalker implements TreeWalker ...@@ -248,11 +248,8 @@ class SqlWalker implements TreeWalker
$qComp = $this->_queryComponents[$dqlAlias]; $qComp = $this->_queryComponents[$dqlAlias];
$columnName = $qComp['metadata']->getColumnName($parts[0]); $columnName = $qComp['metadata']->getColumnName($parts[0]);
$sql = $this->getSqlTableAlias($qComp['metadata']->getTableName(), $dqlAlias) . '.' . $columnName; $sql = $this->getSqlTableAlias($qComp['metadata']->getTableName(), $dqlAlias) . '.' . $columnName;
if ($orderByItem->isAsc()) { $sql .= $orderByItem->isDesc() ? ' DESC' : ' ASC';
$sql .= ' ASC ';
} else if ($orderByItem->isDesc()) {
$sql .= ' DESC ';
}
return $sql; return $sql;
} }
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace Doctrine\Tests\Models\CMS; namespace Doctrine\Tests\Models\CMS;
use Doctrine\Common\Collections\Collection;
/** /**
* @Entity * @Entity
* @Table(name="cms_users") * @Table(name="cms_users")
...@@ -40,11 +42,18 @@ class CmsUser ...@@ -40,11 +42,18 @@ class CmsUser
/** /**
* @ManyToMany(targetEntity="CmsGroup", cascade={"persist"}) * @ManyToMany(targetEntity="CmsGroup", cascade={"persist"})
* @JoinTable(name="cms_users_groups", * @JoinTable(name="cms_users_groups",
joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")}, * joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id")}) * inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/ */
public $groups; public $groups;
public function __construct() {
$this->phonenumbers = new Collection;
$this->articles = new Collection;
$this->groups = new Collection;
}
public function getId() { public function getId() {
return $this->id; return $this->id;
} }
......
...@@ -28,7 +28,9 @@ class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociati ...@@ -28,7 +28,9 @@ class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociati
$this->useModelSet('ecommerce'); $this->useModelSet('ecommerce');
parent::setUp(); parent::setUp();
$this->firstProduct = new ECommerceProduct(); $this->firstProduct = new ECommerceProduct();
$this->firstProduct->setName("First Product");
$this->secondProduct = new ECommerceProduct(); $this->secondProduct = new ECommerceProduct();
$this->secondProduct->setName("Second Product");
$this->firstCategory = new ECommerceCategory(); $this->firstCategory = new ECommerceCategory();
$this->firstCategory->setName("Business"); $this->firstCategory->setName("Business");
$this->secondCategory = new ECommerceCategory(); $this->secondCategory = new ECommerceCategory();
...@@ -102,6 +104,11 @@ class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociati ...@@ -102,6 +104,11 @@ class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociati
$query = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p order by p.id'); $query = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p order by p.id');
$products = $query->getResultList(); $products = $query->getResultList();
$this->assertEquals(2, count($products));
$this->assertEquals(0, count($products[0]->getCategories()->unwrap()));
$this->assertEquals(0, count($products[1]->getCategories()->unwrap()));
$this->assertLoadingOfOwningSide($products); $this->assertLoadingOfOwningSide($products);
} }
...@@ -128,12 +135,17 @@ class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociati ...@@ -128,12 +135,17 @@ class ManyToManyBidirectionalAssociationTest extends AbstractManyToManyAssociati
public function assertLoadingOfOwningSide($products) public function assertLoadingOfOwningSide($products)
{ {
list ($firstProduct, $secondProduct) = $products; list ($firstProduct, $secondProduct) = $products;
$this->assertEquals(2, count($firstProduct->getCategories()));
$this->assertEquals(2, count($secondProduct->getCategories()));
$categories = $firstProduct->getCategories(); $firstProductCategories = $firstProduct->getCategories();
$firstCategoryProducts = $categories[0]->getProducts(); $secondProductCategories = $secondProduct->getCategories();
$secondCategoryProducts = $categories[1]->getProducts();
$this->assertEquals(2, count($firstProductCategories));
$this->assertEquals(2, count($secondProductCategories));
$this->assertTrue($firstProductCategories[0] === $secondProductCategories[0]);
$this->assertTrue($firstProductCategories[1] === $secondProductCategories[1]);
$firstCategoryProducts = $firstProductCategories[0]->getProducts();
$secondCategoryProducts = $firstProductCategories[1]->getProducts();
$this->assertEquals(2, count($firstCategoryProducts)); $this->assertEquals(2, count($firstCategoryProducts));
$this->assertEquals(2, count($secondCategoryProducts)); $this->assertEquals(2, count($secondCategoryProducts));
......
...@@ -70,7 +70,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase ...@@ -70,7 +70,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
{ {
$this->assertSqlGeneration( $this->assertSqlGeneration(
'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id', 'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id',
'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id ASC ' 'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id ASC'
); );
} }
...@@ -78,14 +78,14 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase ...@@ -78,14 +78,14 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
{ {
$this->assertSqlGeneration( $this->assertSqlGeneration(
'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id asc', 'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id asc',
'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id ASC ' 'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id ASC'
); );
} }
public function testSupportsOrderByDesc() public function testSupportsOrderByDesc()
{ {
$this->assertSqlGeneration( $this->assertSqlGeneration(
'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id desc', 'SELECT u FROM Doctrine\Tests\Models\Forum\ForumUser u ORDER BY u.id desc',
'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id DESC ' 'SELECT f0_.id AS id0, f0_.username AS username1 FROM forum_users f0_ ORDER BY f0_.id DESC'
); );
} }
......
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