From df19e68a8613b783591631f64851cc1f6a8c4dbf Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Wed, 7 Dec 2011 11:23:15 -0200 Subject: [PATCH] Fixed DDC-1057 --- lib/Doctrine/ORM/Query/ResultSetMapping.php | 31 ++++++++++++++-- .../ORM/Hydration/ResultSetMappingTest.php | 37 +++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/lib/Doctrine/ORM/Query/ResultSetMapping.php b/lib/Doctrine/ORM/Query/ResultSetMapping.php index 5c6305e25..68713a7cc 100644 --- a/lib/Doctrine/ORM/Query/ResultSetMapping.php +++ b/lib/Doctrine/ORM/Query/ResultSetMapping.php @@ -120,6 +120,7 @@ class ResultSetMapping * results or joined entity results within this ResultSetMapping. * @param string $resultAlias The result alias with which the entity result should be * placed in the result structure. + * @return ResultSetMapping This ResultSetMapping instance. * * @todo Rename: addRootEntity */ @@ -131,6 +132,8 @@ class ResultSetMapping if ($resultAlias !== null) { $this->isMixed = true; } + + return $this; } /** @@ -141,13 +144,16 @@ class ResultSetMapping * @param string $alias The alias of the entity result or joined entity result the discriminator * column should be used for. * @param string $discrColumn The name of the discriminator column in the SQL result set. - * + * @return ResultSetMapping This ResultSetMapping instance. + * * @todo Rename: addDiscriminatorColumn */ public function setDiscriminatorColumn($alias, $discrColumn) { $this->discriminatorColumns[$alias] = $discrColumn; $this->columnOwnerMap[$discrColumn] = $alias; + + return $this; } /** @@ -155,6 +161,7 @@ class ResultSetMapping * * @param string $alias The alias of an entity result or joined entity result. * @param string $fieldName The name of the field to use for indexing. + * @return ResultSetMapping This ResultSetMapping instance. */ public function addIndexBy($alias, $fieldName) { @@ -168,6 +175,8 @@ class ResultSetMapping break; } + + return $this; /* TODO: check if this exception can be put back, for now it's gone because of assumptions made by some ORM internals if ( ! $found) { @@ -186,11 +195,13 @@ class ResultSetMapping * Set to index by a scalar result column name * * @param $resultColumnName - * @return void + * @return ResultSetMapping This ResultSetMapping instance. */ public function addIndexByScalar($resultColumnName) { $this->indexByMap['scalars'] = $resultColumnName; + + return $this; } /** @@ -198,11 +209,13 @@ class ResultSetMapping * * @param $alias * @param $resultColumnName - * @return void + * @return ResultSetMapping This ResultSetMapping instance. */ public function addIndexByColumn($alias, $resultColumnName) { $this->indexByMap[$alias] = $resultColumnName; + + return $this; } /** @@ -242,6 +255,7 @@ class ResultSetMapping * the field $fieldName is defined on a subclass, specify that here. * If not specified, the field is assumed to belong to the class * designated by $alias. + * @return ResultSetMapping This ResultSetMapping instance. * @todo Rename: addField */ public function addFieldResult($alias, $columnName, $fieldName, $declaringClass = null) @@ -256,6 +270,8 @@ class ResultSetMapping if ( ! $this->isMixed && $this->scalarMappings) { $this->isMixed = true; } + + return $this; } /** @@ -265,6 +281,7 @@ class ResultSetMapping * @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. + * @return ResultSetMapping This ResultSetMapping instance. * @todo Rename: addJoinedEntity */ public function addJoinedEntityResult($class, $alias, $parentAlias, $relation) @@ -272,6 +289,8 @@ class ResultSetMapping $this->aliasMap[$alias] = $class; $this->parentAliasMap[$alias] = $parentAlias; $this->relationMap[$alias] = $relation; + + return $this; } /** @@ -279,6 +298,7 @@ class ResultSetMapping * * @param string $columnName The name of the column in the SQL result set. * @param string $alias The result alias with which the scalar result should be placed in the result structure. + * @return ResultSetMapping This ResultSetMapping instance. * @todo Rename: addScalar */ public function addScalarResult($columnName, $alias) @@ -288,6 +308,8 @@ class ResultSetMapping if ( ! $this->isMixed && $this->fieldMappings) { $this->isMixed = true; } + + return $this; } /** @@ -439,6 +461,7 @@ class ResultSetMapping * @param string $columnName * @param string $fieldName * @param bool + * @return ResultSetMapping This ResultSetMapping instance. */ public function addMetaResult($alias, $columnName, $fieldName, $isIdentifierColumn = false) { @@ -448,5 +471,7 @@ class ResultSetMapping if ($isIdentifierColumn) { $this->isIdentifierColumn[$alias][$columnName] = true; } + + return $this; } } diff --git a/tests/Doctrine/Tests/ORM/Hydration/ResultSetMappingTest.php b/tests/Doctrine/Tests/ORM/Hydration/ResultSetMappingTest.php index 562fd4d87..d18c4dbb7 100644 --- a/tests/Doctrine/Tests/ORM/Hydration/ResultSetMappingTest.php +++ b/tests/Doctrine/Tests/ORM/Hydration/ResultSetMappingTest.php @@ -14,7 +14,14 @@ require_once __DIR__ . '/../../TestInit.php'; */ class ResultSetMappingTest extends \Doctrine\Tests\OrmTestCase { + /** + * @var ResultSetMapping + */ private $_rsm; + + /** + * @var \Doctrine\ORM\EntityManager + */ private $_em; protected function setUp() { @@ -56,5 +63,35 @@ class ResultSetMappingTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals('username', $this->_rsm->getFieldName('username')); $this->assertEquals('name', $this->_rsm->getFieldName('name')); } + + /** + * @group DDC-1057 + * + * Fluent interface test, not a real result set mapping + */ + public function testFluentInterface() + { + $rms = $this->_rsm; + + $rms->addEntityResult('Doctrine\Tests\Models\CMS\CmsUser','u') + ->addJoinedEntityResult('Doctrine\Tests\Models\CMS\CmsPhonenumber','p','u','phonenumbers') + ->addFieldResult('u', 'id', 'id') + ->addFieldResult('u', 'name', 'name') + ->setDiscriminatorColumn('name', 'name') + ->addIndexByColumn('id', 'id') + ->addIndexBy('username', 'username') + ->addIndexByScalar('sclr0') + ->addScalarResult('sclr0', 'numPhones') + ->addMetaResult('a', 'user_id', 'user_id'); + + + $this->assertTrue($rms->hasIndexBy('id')); + $this->assertTrue($rms->isFieldResult('id')); + $this->assertTrue($rms->isFieldResult('name')); + $this->assertTrue($rms->isScalarResult('sclr0')); + $this->assertTrue($rms->isRelation('p')); + $this->assertTrue($rms->hasParentAlias('p')); + $this->assertTrue($rms->isMixedResult()); + } }