Permalink
Browse files

Merge pull request #554 from beregond/hydrator-fix

Fixed ObjectHydrator when namespace alias is given.
  • Loading branch information...
2 parents abc3ba0 + 92ada24 commit dea37ed9e84922f37ef21740671e5e29c94454d3 @guilhermeblanco guilhermeblanco committed Feb 2, 2013
@@ -296,6 +296,7 @@ public function processParameterValue($value)
*/
public function setResultSetMapping(Query\ResultSetMapping $rsm)
{
+ $rsm->translateNamespaces($this->_em);
$this->_resultSetMapping = $rsm;
return $this;
@@ -83,7 +83,7 @@ class ObjectHydrator extends AbstractHydrator
*/
private $existingCollections = array();
- /**
+ /**
* {@inheritdoc}
*/
protected function prepare()
@@ -525,7 +525,7 @@ protected function hydrateRowData(array $row, array &$cache, array &$result)
// check for existing result from the iterations before
if ( ! isset($this->identifierMap[$dqlAlias][$id[$dqlAlias]])) {
$element = $this->getEntity($rowData[$dqlAlias], $dqlAlias);
-
+
if ($this->_rsm->isMixed) {
$element = array($entityKey => $element);
}
@@ -597,7 +597,7 @@ protected function hydrateRowData(array $row, array &$cache, array &$result)
if ($count === 1) {
$result[$resultKey] = $obj;
-
+
continue;
}
@@ -19,6 +19,8 @@
namespace Doctrine\ORM\Query;
+use Doctrine\ORM\EntityManager;
+
/**
* A ResultSetMapping describes how a result set of an SQL query maps to a Doctrine result.
*
@@ -543,4 +545,27 @@ public function addMetaResult($alias, $columnName, $fieldName, $isIdentifierColu
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 ( ! isset($fqcn[$alias])) {
+ $fqcn[$alias] = $em->getClassMetadata($alias)->getName();
+ }
+ $alias = $fqcn[$alias];
+ };
+
+ array_walk($this->aliasMap, $translate);
+ array_walk($this->declaringClasses, $translate);
+ }
}
+
@@ -0,0 +1,115 @@
+<?php
+
+namespace Doctrine\Tests\ORM\Functional\Ticket;
+
+use Doctrine\ORM\Query\ResultSetMapping;
+use Doctrine\ORM\Query\ResultSetMappingBuilder;
+
+/**
+ * @group DDC-2256
+ */
+class DDC2256Test extends \Doctrine\Tests\OrmFunctionalTestCase
+{
+ protected function setup()
+ {
+ parent::setup();
+ $this->_schemaTool->createSchema(array(
+ $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2256User'),
+ $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2256Group')
+ ));
+ }
+
+ public function testIssue()
+ {
+ $config = $this->_em->getConfiguration();
+ $config->addEntityNamespace('MyNamespace', 'Doctrine\Tests\ORM\Functional\Ticket');
+
+ $user = new DDC2256User();
+ $user->name = 'user';
+ $group = new DDC2256Group();
+ $group->name = 'group';
+ $user->group = $group;
+
+ $this->_em->persist($user);
+ $this->_em->persist($group);
+ $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->addEntityResult('MyNamespace:DDC2256User', 'u');
+ $rsm->addFieldResult('u', 'id', 'id');
+ $rsm->addFieldResult('u', 'name', 'name');
+
+ $rsm->addJoinedEntityResult('MyNamespace:DDC2256Group', 'g', 'u', 'group');
+ $rsm->addFieldResult('g', 'group_id', 'id');
+ $rsm->addFieldResult('g', 'group_name', 'name');
+
+ $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();
+ }
+}
+
+/**
+ * @Entity
+ * @Table(name="ddc2256_users")
+ */
+class DDC2256User
+{
+ /**
+ * @Id
+ * @Column(type="integer")
+ * @GeneratedValue(strategy="AUTO")
+ */
+ public $id;
+
+ /**
+ * @Column(type="string")
+ */
+ public $name;
+
+ /**
+ * @ManyToOne(targetEntity="DDC2256Group", inversedBy="users")A
+ * @JoinColumn(name="group_id")
+ */
+ public $group;
+}
+
+/**
+ * @Entity
+ * @Table(name="ddc2256_groups")
+ */
+class DDC2256Group
+{
+ /**
+ * @Id
+ * @Column(type="integer")
+ * @GeneratedValue(strategy="AUTO")
+ */
+ public $id;
+
+ /**
+ * @Column(type="string")
+ */
+ public $name;
+
+ /**
+ * @OneToMany(targetEntity="DDC2256User", mappedBy="group")
+ */
+ public $users;
+
+ public function __construct()
+ {
+ $this->users = new \Doctrine\Common\Collections\ArrayCollection();
+ }
+}
+

0 comments on commit dea37ed

Please sign in to comment.