DDC-1362: Native SQL with addJoinedEntityResult() causes wrong mapping #1709

Open
opened 2026-01-22 13:22:56 +01:00 by admin · 0 comments
Owner

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

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
admin added the Bug label 2026-01-22 13:22:56 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1709