From c46d83514631841800c782890ad27a2b77de200f Mon Sep 17 00:00:00 2001 From: Michael Ridgway Date: Wed, 30 Mar 2011 10:27:31 -0400 Subject: [PATCH] Moved new functions to ResultSetMappingBuilder class --- lib/Doctrine/ORM/Query/ResultSetMapping.php | 37 +-------- .../ORM/Query/ResultSetMappingBuilder.php | 80 +++++++++++++++++++ .../Tests/ORM/Functional/NativeQueryTest.php | 21 +++-- 3 files changed, 90 insertions(+), 48 deletions(-) create mode 100644 lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php diff --git a/lib/Doctrine/ORM/Query/ResultSetMapping.php b/lib/Doctrine/ORM/Query/ResultSetMapping.php index ededf1153..1de2d4459 100644 --- a/lib/Doctrine/ORM/Query/ResultSetMapping.php +++ b/lib/Doctrine/ORM/Query/ResultSetMapping.php @@ -392,39 +392,4 @@ class ResultSetMapping $this->metaMappings[$columnName] = $fieldName; $this->columnOwnerMap[$columnName] = $alias; } - - /** - * Adds a root entity and all of its fields to the result set. - * - * @param ClassMetadata $classMetadata - * @param string $alias The unique alias to use for the root entity. - * @param string $prefix Prefix for columns - */ - public function addRootEntityFromClassMetadata(ClassMetadata $classMetadata, $alias, $prefix = '') - { - $this->addEntityResult($classMetadata->getReflectionClass()->getName(), $alias); - foreach ($classMetadata->getColumnNames() AS $columnName) { - // $columnName should use DBAL\Platforms\AbstractPlatform::getSQLResultCasing() but we don't have the EM - $this->addFieldResult($alias, $prefix . $columnName, $classMetadata->getFieldName($columnName)); - } - } - - /** - * Adds a joined entity and all of its fields to the result set. - * - * @param ClassMetadata $classMetadata - * @param string $alias The unique alias to use for the joined entity. - * @param string $parentAlias The alias of the entity result that is the parent of this joined result. - * @param object $relation The association field that connects the parent entity result with the joined entity result. - * @param string $prefix Prefix for columns - */ - public function addJoinedEntityFromClassMetadata(ClassMetadata $classMetadata, $alias, $parentAlias, $relation, $prefix = '') - { - $this->addJoinedEntityResult($classMetadata->getReflectionClass()->getName(), $alias, $parentAlias, $relation); - foreach ($classMetadata->getColumnNames() AS $columnName) { - // $columnName should use DBAL\Platforms\AbstractPlatform::getSQLResultCasing() but we don't have the EM - $this->addFieldResult($alias, $prefix . $columnName, $classMetadata->getFieldName($columnName)); - } - } -} - +} \ No newline at end of file diff --git a/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php b/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php new file mode 100644 index 000000000..517fd0036 --- /dev/null +++ b/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php @@ -0,0 +1,80 @@ +. + */ + +namespace Doctrine\ORM\Query; + +use Doctrine\ORM\EntityManager; + +/** + * A ResultSetMappingBuilder uses the EntityManager to automatically populate entity fields + * + * @author Michael Ridgway + * @since 2.1 + */ +class ResultSetMappingBuilder extends ResultSetMapping +{ + /** + * @var EntityManager + */ + private $em; + + /** + * @param EntityManager + */ + public function __construct(EntityManager $em) + { + $this->em = $em; + } + + /** + * Adds a root entity and all of its fields to the result set. + * + * @param string $class The class name of the root entity. + * @param string $alias The unique alias to use for the root entity. + * @param string $prefix Prefix for columns + */ + public function addRootEntityFromClassMetadata($class, $alias, $prefix = '') + { + $this->addEntityResult($class, $alias); + $classMetadata = $this->em->getClassMetadata($class); + $platform = $this->em->getConnection()->getDatabasePlatform(); + foreach ($classMetadata->getColumnNames() AS $columnName) { + $this->addFieldResult($alias, $platform->getSQLResultCasing($prefix . $columnName), $classMetadata->getFieldName($columnName)); + } + } + + /** + * Adds a joined entity and all of its fields to the result set. + * + * @param string $class The class name of the joined entity. + * @param string $alias The unique alias to use for the joined entity. + * @param string $parentAlias The alias of the entity result that is the parent of this joined result. + * @param object $relation The association field that connects the parent entity result with the joined entity result. + * @param string $prefix Prefix for columns + */ + public function addJoinedEntityFromClassMetadata($class, $alias, $parentAlias, $relation, $prefix = '') + { + $this->addJoinedEntityResult($class, $alias, $parentAlias, $relation); + $classMetadata = $this->em->getClassMetadata($class); + $platform = $this->em->getConnection()->getDatabasePlatform(); + foreach ($classMetadata->getColumnNames() AS $columnName) { + $this->addFieldResult($alias, $platform->getSQLResultCasing($prefix . $columnName), $classMetadata->getFieldName($columnName)); + } + } +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php b/tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php index 65778badf..1b1a713b8 100644 --- a/tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php @@ -3,6 +3,7 @@ namespace Doctrine\Tests\ORM\Functional; use Doctrine\ORM\Query\ResultSetMapping; +use Doctrine\ORM\Query\ResultSetMappingBuilder; use Doctrine\Tests\Models\CMS\CmsUser; use Doctrine\Tests\Models\CMS\CmsPhonenumber; use Doctrine\Tests\Models\CMS\CmsAddress; @@ -157,7 +158,7 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertSame($q, $q2); } - public function testJoinedOneToManyNativeQueryWithRSMHelper() + public function testJoinedOneToManyNativeQueryWithRSMBuilder() { $user = new CmsUser; $user->name = 'Roman'; @@ -174,11 +175,9 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->clear(); - $rsm = new ResultSetMapping; - $userMetadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); - $phoneMetadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber'); - $rsm->addRootEntityFromClassMetadata($userMetadata, 'u'); - $rsm->addJoinedEntityFromClassMetadata($phoneMetadata, 'p', 'u', 'phonenumbers', 'p_'); + $rsm = new ResultSetMappingBuilder($this->_em); + $rsm->addRootEntityFromClassMetadata('Doctrine\Tests\Models\CMS\CmsUser', 'u'); + $rsm->addJoinedEntityFromClassMetadata('Doctrine\Tests\Models\CMS\CmsPhonenumber', 'p', 'u', 'phonenumbers', 'p_'); $query = $this->_em->createNativeQuery('SELECT u.*, p.phonenumber AS p_phonenumber FROM cms_users u INNER JOIN cms_phonenumbers p ON u.id = p.user_id WHERE username = ?', $rsm); $query->setParameter(1, 'romanb'); @@ -194,7 +193,7 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertTrue($phones[0]->getUser() === $users[0]); } - public function testJoinedOneToOneNativeQueryWithRSMHelper() + public function testJoinedOneToOneNativeQueryWithRSMBuilder() { $user = new CmsUser; $user->name = 'Roman'; @@ -215,11 +214,9 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->clear(); - $rsm = new ResultSetMapping; - $userMetadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); - $addressMetadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'); - $rsm->addRootEntityFromClassMetadata($userMetadata, 'u'); - $rsm->addJoinedEntityFromClassMetadata($addressMetadata, 'a', 'u', 'address', 'a_'); + $rsm = new ResultSetMappingBuilder($this->_em); + $rsm->addRootEntityFromClassMetadata('Doctrine\Tests\Models\CMS\CmsUser', 'u'); + $rsm->addJoinedEntityFromClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress', 'a', 'u', 'address', 'a_'); $query = $this->_em->createNativeQuery('SELECT u.id, u.name, u.status, a.id AS a_id, a.country AS a_country, a.zip AS a_zip, a.city AS a_city FROM cms_users u INNER JOIN cms_addresses a ON u.id = a.user_id WHERE u.username = ?', $rsm); $query->setParameter(1, 'romanb');