diff --git a/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php b/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php index da7ce2565..ad159e0a5 100644 --- a/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php +++ b/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php @@ -168,7 +168,12 @@ class ResultSetMappingBuilder extends ResultSetMapping throw new \InvalidArgumentException("The column '$columnAlias' conflicts with another column in the mapper."); } - $this->addMetaResult($alias, $columnAlias, $columnName); + $this->addMetaResult( + $alias, + $columnAlias, + $columnName, + (isset($associationMapping['id']) && $associationMapping['id'] === true) + ); } } } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2660Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2660Test.php new file mode 100644 index 000000000..78ffdeb4f --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2660Test.php @@ -0,0 +1,122 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2660Product'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2660Customer'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2660CustomerOrder') + )); + } catch(\Exception $e) { + return; + } + + for ($i = 0; $i < 5; $i++) { + $product = new DDC2660Product(); + $customer = new DDC2660Customer(); + $order = new DDC2660CustomerOrder($product, $customer, 'name' . $i); + + $this->_em->persist($product); + $this->_em->persist($customer); + $this->_em->flush(); + + $this->_em->persist($order); + $this->_em->flush(); + } + + $this->_em->clear(); + } + + public function testIssueWithExtraColumn() + { + $sql = "SELECT o.product_id, o.customer_id, o.name FROM ddc_2660_customer_order o"; + + $rsm = new ResultSetMappingBuilder($this->_getEntityManager()); + $rsm->addRootEntityFromClassMetadata(__NAMESPACE__ . '\DDC2660CustomerOrder', 'c'); + + $query = $this->_em->createNativeQuery($sql, $rsm); + $result = $query->getResult(); + + $this->assertCount(5, $result); + + foreach ($result as $order) { + $this->assertNotNull($order); + $this->assertInstanceOf(__NAMESPACE__ . '\\DDC2660CustomerOrder', $order); + } + } + + public function testIssueWithoutExtraColumn() + { + $sql = "SELECT o.product_id, o.customer_id FROM ddc_2660_customer_order o"; + + $rsm = new ResultSetMappingBuilder($this->_getEntityManager()); + $rsm->addRootEntityFromClassMetadata(__NAMESPACE__ . '\DDC2660CustomerOrder', 'c'); + + $query = $this->_em->createNativeQuery($sql, $rsm); + $result = $query->getResult(); + + $this->assertCount(5, $result); + + foreach ($result as $order) { + $this->assertNotNull($order); + $this->assertInstanceOf(__NAMESPACE__ . '\\DDC2660CustomerOrder', $order); + } + } +} +/** + * @Entity @Table(name="ddc_2660_product") + */ +class DDC2660Product +{ + /** @Id @Column(type="integer") @GeneratedValue */ + public $id; +} + +/** @Entity @Table(name="ddc_2660_customer") */ +class DDC2660Customer +{ + /** @Id @Column(type="integer") @GeneratedValue */ + public $id; +} + +/** @Entity @Table(name="ddc_2660_customer_order") */ +class DDC2660CustomerOrder +{ + /** + * @Id @ManyToOne(targetEntity="DDC2660Product") + */ + public $product; + + /** + * @Id @ManyToOne(targetEntity="DDC2660Customer") + */ + public $customer; + + /** + * @Column(type="string") + */ + public $name; + + public function __construct(DDC2660Product $product, DDC2660Customer $customer, $name) + { + $this->product = $product; + $this->customer = $customer; + $this->name = $name; + } +}