diff --git a/lib/Doctrine/ODM/PHPCR/Query/Builder/BuilderConverterPhpcr.php b/lib/Doctrine/ODM/PHPCR/Query/Builder/BuilderConverterPhpcr.php index dafbe387d..4313e1f71 100644 --- a/lib/Doctrine/ODM/PHPCR/Query/Builder/BuilderConverterPhpcr.php +++ b/lib/Doctrine/ODM/PHPCR/Query/Builder/BuilderConverterPhpcr.php @@ -640,9 +640,18 @@ protected function walkOperandDynamicField(OperandDynamicField $node) $field = $node->getField(); $classMeta = $this->aliasMetadata[$alias]; + + if ($field === $classMeta->nodename) { + throw new InvalidArgumentException(sprintf( + 'It is not possible to order by a nodename property "%s->%s"', + $classMeta->name, + $field + )); + } + if ($classMeta->hasAssociation($field)) { throw new InvalidArgumentException(sprintf( - 'It is not possible to filter on association fields %s->%s', + 'It is not possible to order by association field "%s->%s"', $classMeta->name, $field )); @@ -760,24 +769,6 @@ protected function walkOrderBy(OrderBy $node) foreach ($orderings as $ordering) { $node = $ordering->getChild(); $alias = $node->getAlias(); - $field = $node->getField(); - - $classMeta = $this->aliasMetadata[$alias]; - if ($field === $classMeta->nodename) { - throw new InvalidArgumentException(sprintf( - 'It is not possible to order by a nodename property "%s->%s"', - $classMeta->name, - $field - )); - } - - if ($classMeta->hasAssociation($field)) { - throw new InvalidArgumentException(sprintf( - 'It is not possible to order by association field "%s->%s"', - $classMeta->name, - $field - )); - } $dynOp = $ordering->getChildOfType( QBConstants::NT_OPERAND_DYNAMIC diff --git a/tests/Doctrine/Tests/ODM/PHPCR/Query/Builder/BuilderConverterPhpcrTest.php b/tests/Doctrine/Tests/ODM/PHPCR/Query/Builder/BuilderConverterPhpcrTest.php index 8bad71d97..9575f995b 100644 --- a/tests/Doctrine/Tests/ODM/PHPCR/Query/Builder/BuilderConverterPhpcrTest.php +++ b/tests/Doctrine/Tests/ODM/PHPCR/Query/Builder/BuilderConverterPhpcrTest.php @@ -63,6 +63,19 @@ public function setUp() ->method('getName') ->will($me->returnValue($documentFqn)); + $meta->expects($me->any()) + ->method('hasAssociation') + ->will($me->returnCallback(function ($field) { + if ($field === 'associationfield') { + return true; + } + + return false; + })); + + $meta->nodename = 'nodenameProperty'; + $meta->name = 'MyClassName'; + return $meta; })); @@ -480,7 +493,6 @@ public function provideTestDispatchOperands() 'phpcr_class' => 'LiteralInterface', )), ); - } /** @@ -547,6 +559,37 @@ public function testOrderBy() $this->assertCount(2, $res); } + public function provideOrderByDynamicField() + { + return array( + array('alias_1.ok_field', null), + array('alias_1.nodenameProperty', 'It is not possible to order by a nodename property "MyClassName->nodenameProperty"'), + array('alias_1.associationfield', 'It is not possible to order by association field "MyClassName->associationfield"'), + ); + } + + /** + * @dataProvider provideOrderByDynamicField + */ + public function testOrderByDynamicField($field, $exception) + { + $this->primeBuilder(); + $order1 = $this->createNode('Ordering', array(QOMConstants::JCR_ORDER_ASCENDING)); + + $orderBy = $this->createNode('OrderBy', array()); + $orderBy->addChild($order1); + + $op = $this->createNode('OperandDynamicField', array($field)); + $order1->addChild($op); + + if (null !== $exception) { + $this->setExpectedException('Doctrine\ODM\PHPCR\Exception\InvalidArgumentException', $exception); + } + + $res = $this->converter->dispatch($orderBy); + $this->assertCount(1, $res); + } + public function testGetQuery() { $me = $this;