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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Changelog


* **2014-08-07**: properly handle fields mapped to nodename in DocumentRepository::findBy()
Report error when trying to find or order by association fields, instead of building
queries that won't work.

1.2.0-RC1
---------
Expand Down
27 changes: 22 additions & 5 deletions lib/Doctrine/ODM/PHPCR/DocumentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

use Doctrine\ODM\PHPCR\Query\Builder\ConstraintFactory;


/**
* A DocumentRepository serves as a repository for documents with generic as well as
* business specific methods for retrieving documents.
Expand Down Expand Up @@ -147,10 +146,19 @@ public function findBy(array $criteria, array $orderBy = null, $limit = null, $o
$orderByNode = $qb->orderBy();

if ($orderBy) {
$classMeta = $this->getClassMetadata();
foreach ($orderBy as $field => $order) {
if ($field === $this->getClassMetadata()->nodename) {
if ($field === $classMeta->nodename) {
throw new InvalidArgumentException(sprintf(
'It is not possible to order by a nodename property "%s->%s"',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is not possible, then should we be doing this further back in the query builder converter?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

someone should actually try to confirm if its not possible

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like it is not possible to use functions in an ORDER BY http://www.h2database.com/jcr/grammar.html#name

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but .. this is not a Document Repository problem right? Shouldn't this logic be in QueryBuilder? We would presumeably get the same problem if we use the QueryBuilder directly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes ..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is not possible, then should we be doing this further back in the query builder converter?

So this is not the right place? Can we revert it?

$classMeta->name,
$field
));
}
if ($classMeta->hasAssociation($field)) {
throw new InvalidArgumentException(sprintf(
'It is not possible to order by a nodename property "%s"',
'It is not possible to order by association field "%s->%s"',
$classMeta->name,
$field
));
}
Expand Down Expand Up @@ -186,14 +194,23 @@ public function findBy(array $criteria, array $orderBy = null, $limit = null, $o

/**
* Constraints a field for a given value
*
*
* @param ConstraintFactory $where
* @param string $field The field searched
* @param mixed $value The value to search for
* @param string The alias used
* @param string $alias The alias used
*/
protected function constraintField(ConstraintFactory $where, $field, $value, $alias)
{
$classMeta = $this->getClassMetadata();
if ($classMeta->hasAssociation($field)) {
throw new InvalidArgumentException(sprintf(
'It is not possible to filter on association fields %s->%s',
$classMeta->name,
$field
));
}

if ($field === $this->getClassMetadata()->nodename) {
$where->eq()->name($alias)->literal($value);
} else {
Expand Down
43 changes: 0 additions & 43 deletions tests/Doctrine/Tests/ODM/PHPCR/DocumentRepositoryTest.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

use Doctrine\Tests\Models\CMS\CmsTeamUser;
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\ODM\PHPCR\PHPCRFunctionalTestCase;
use Doctrine\ODM\PHPCR\Query\Builder\AbstractNode as QBConstants;

/**
* @group functional
*/
class DocumentRepositoryTest extends \Doctrine\Tests\ODM\PHPCR\PHPCRFunctionalTestCase
class DocumentRepositoryTest extends PHPCRFunctionalTestCase
{
/**
* @var \Doctrine\ODM\PHPCR\DocumentManager
Expand Down Expand Up @@ -40,6 +42,22 @@ public function setUp()
$session->save();
}

public function testCreateQueryBuilder()
{
$rep = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
$qb = $rep->createQueryBuilder('a');

$from = $qb->getChildOfType(QBConstants::NT_FROM);
$this->assertInstanceOf('Doctrine\ODM\PHPCR\Query\Builder\From', $from);
$source = $from->getChildOfType(QBConstants::NT_SOURCE);
$this->assertInstanceOf('Doctrine\ODM\PHPCR\Query\Builder\SourceDocument', $source);

$this->assertEquals('a', $source->getAlias());
$this->assertEquals('a', $qb->getPrimaryAlias());

$this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $source->getDocumentFqn());
}

public function testLoadMany()
{
$user1 = new CmsUser();
Expand Down Expand Up @@ -140,6 +158,30 @@ public function testFindByOnNodename()
$this->assertEquals($user->username, $users['/functional/lsmith/beberlei']->username);
}

/**
* @expectedException \Doctrine\ODM\PHPCR\Exception\InvalidArgumentException
*/
public function testFindByOrderNodename()
{
$this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsTeamUser')->findBy(array('nodename' =>'beberlei'), array('nodename'));
}

/**
* @expectedException \Doctrine\ODM\PHPCR\Exception\InvalidArgumentException
*/
public function testFindByOnAssociation()
{
$this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsTeamUser')->findBy(array('parent' =>'/foo'));
}

/**
* @expectedException \Doctrine\ODM\PHPCR\Exception\InvalidArgumentException
*/
public function testFindByOrderAssociation()
{
$this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsTeamUser')->findBy(array('nodename' =>'beberlei'), array('parent'));
}

public function testFindOneBy()
{
$user1 = new CmsUser();
Expand Down