1
0
Fork 0
mirror of synced 2025-04-03 13:23:37 +03:00

[DDC-2256] Moved aliases translation to ResultSetMapping, fixed tests.

This commit is contained in:
Szczepan Cieślik 2013-01-25 12:06:13 +01:00
parent 1949ff8602
commit d7f82221d1
4 changed files with 51 additions and 13 deletions

View file

@ -296,6 +296,7 @@ abstract class AbstractQuery
*/ */
public function setResultSetMapping(Query\ResultSetMapping $rsm) public function setResultSetMapping(Query\ResultSetMapping $rsm)
{ {
$rsm->translateNamespaces($this->_em);
$this->_resultSetMapping = $rsm; $this->_resultSetMapping = $rsm;
return $this; return $this;

View file

@ -102,12 +102,6 @@ class ObjectHydrator extends AbstractHydrator
$this->identifierMap[$dqlAlias] = array(); $this->identifierMap[$dqlAlias] = array();
$this->idTemplate[$dqlAlias] = ''; $this->idTemplate[$dqlAlias] = '';
// Check for namespace alias.
if (strpos($className, ':') !== false) {
$metadata = $this->_em->getClassMetadata($className);
$className = $metadata->name;
}
if ( ! isset($this->ce[$className])) { if ( ! isset($this->ce[$className])) {
$this->ce[$className] = $this->_em->getClassMetadata($className); $this->ce[$className] = $this->_em->getClassMetadata($className);
} }

View file

@ -19,6 +19,8 @@
namespace Doctrine\ORM\Query; namespace Doctrine\ORM\Query;
use Doctrine\ORM\EntityManager;
/** /**
* A ResultSetMapping describes how a result set of an SQL query maps to a Doctrine result. * A ResultSetMapping describes how a result set of an SQL query maps to a Doctrine result.
* *
@ -543,4 +545,33 @@ class ResultSetMapping
return $this; return $this;
} }
/**
* Allows to translate entity namespaces to full qualified names.
*
* @param EntityManager $em
*
* @return void
*/
public function translateNamespaces(EntityManager $em)
{
$fqcn = array();
$translate = function (&$alias) use ($em, $fqcn)
{
if (strpos($alias, ':') !== false && !isset($fqcn[$alias])) {
if ($metadata = $em->getClassMetadata($alias)) {
$fqcn[$alias] = $metadata->name;
}
}
if (isset($fqcn[$alias])) {
$alias = $fqcn[$alias];
}
};
array_walk($this->aliasMap, $translate);
array_walk($this->declaringClasses, $translate);
}
} }

View file

@ -5,6 +5,7 @@ namespace Doctrine\Tests\ORM\Functional\Ticket;
require_once __DIR__ . '/../../../TestInit.php'; require_once __DIR__ . '/../../../TestInit.php';
use Doctrine\ORM\Query\ResultSetMapping; use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\Query\ResultSetMappingBuilder;
/** /**
* @group DDC-2256 * @group DDC-2256
@ -34,18 +35,29 @@ class DDC2256Test extends \Doctrine\Tests\OrmFunctionalTestCase
$this->_em->persist($user); $this->_em->persist($user);
$this->_em->persist($group); $this->_em->persist($group);
$this->_em->flush(); $this->_em->flush();
$this->_em->clear();
$sql = 'SELECT u.id, u.name, g.id as group_id, g.name as group_name FROM ddc2256_users u LEFT JOIN ddc2256_groups g ON u.group_id = g.id';
// Test ResultSetMapping.
$rsm = new ResultSetMapping(); $rsm = new ResultSetMapping();
$rsm->addEntityResult('MyNamespace:DDC2256Group', 'g'); $rsm->addEntityResult('MyNamespace:DDC2256User', 'u');
$rsm->addFieldResult('g', 'id', 'id'); $rsm->addFieldResult('u', 'id', 'id');
$rsm->addFieldResult('g', 'name', 'name'); $rsm->addFieldResult('u', 'name', 'name');
$rsm->addJoinedEntityResult('MyNamespace:DDC2256User', 'u', 'g', 'users'); $rsm->addJoinedEntityResult('MyNamespace:DDC2256Group', 'g', 'u', 'group');
$rsm->addFieldResult('u', 'user_id', 'id'); $rsm->addFieldResult('g', 'group_id', 'id');
$rsm->addFieldResult('u', 'user_name', 'name'); $rsm->addFieldResult('g', 'group_name', 'name');
$this->_em->createNativeQuery('SELECT g.id, g.name, u.id as user_id, u.name as user_name FROM ddc2256_groups g LEFT JOIN ddc2256_users u ON u.group_id = g.id', $rsm)->getResult(); $this->_em->createNativeQuery($sql, $rsm)->getResult();
// Test ResultSetMappingBuilder.
$rsm = new ResultSetMappingBuilder($this->_em);
$rsm->addRootEntityFromClassMetadata('MyNamespace:DDC2256User', 'u');
$rsm->addJoinedEntityFromClassMetadata('MyNamespace:DDC2256Group', 'g', 'u', 'group', array('id' => 'group_id', 'name' => 'group_name'));
$this->_em->createNativeQuery($sql, $rsm)->getResult();
} }
} }