Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions lib/Doctrine/ODM/PHPCR/Query/Builder/BuilderConverterPhpcr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}));

Expand Down Expand Up @@ -480,7 +493,6 @@ public function provideTestDispatchOperands()
'phpcr_class' => 'LiteralInterface',
)),
);

}

/**
Expand Down Expand Up @@ -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;
Expand Down