mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
DDC-1362: Native SQL with addJoinedEntityResult() causes wrong mapping #1709
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @doctrinebot on GitHub (Sep 5, 2011).
Originally assigned to: @beberlei on GitHub.
Jira issue originally created by user ramandv:
Order of fields in the native select query and order of result set mapping should be in the same order, otherwise wrong mapping is happening.
I have faced problem with my code. I am explaining the issue with the simple code
$sql ="
SELECT
a.id as aid,
a.city_name,
a.country_name,
u.id as uid,
u.name,
u.age
FROM
user as u
JOIN
address as a
ON
u.address_id=a.id";
$rsm = new ResultSetMapping;
$rsm->addEntityResult('models\User', 'u');
$rsm->addFieldResult('u', 'uid', 'id');
$rsm->addFieldResult('u', 'name', 'name');
$rsm->addFieldResult('u', 'age', 'age');
$rsm->addJoinedEntityResult('models\Address', 'a', 'u', 'address');
$rsm->addFieldResult('a', 'aid', 'id');
$rsm->addFieldResult('a', 'city_name', 'city_name');
$rsm->addFieldResult('a', 'country_name', 'country_name');
$query = $this->_em->createNativeQuery($sql, $rsm);
return $query->getResult();
As in the code, the select clause column ordering and result set mapping orders are reversed. It causes the wrong mapping as follows
The wrong mapping happens as follows
user1 => address2
user2 => address3
user3 => null
[NOTE: address1 missing and see the wrong mapping]
instead of the correct mapping as
user1=>address1
user2=>address2
user3=>address3