Skip to content

Commit

Permalink
changed findBy to use getDocumentsByQuery, testing findOneBy
Browse files Browse the repository at this point in the history
  • Loading branch information
nacmartin committed Feb 8, 2012
1 parent dfb6c70 commit 85f8752
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
11 changes: 3 additions & 8 deletions lib/Doctrine/ODM/PHPCR/DocumentRepository.php
Expand Up @@ -159,13 +159,7 @@ public function findBy(array $criteria, array $orderBy = null, $limit = null, $o
foreach ($criteria as $field => $value) {
$qb->andWhere($qf->comparison($qf->propertyValue($field), Constants::JCR_OPERATOR_EQUAL_TO, $qf->literal($value)));
}
$nodes = $qb->execute()->getNodes(true);

$documents = array();

foreach ($nodes as $path => $node) {
$documents[$path] = $this->uow->createDocument($this->className, $node);
}
$documents = $this->getDocumentsByQuery($qb->getQuery());

return $documents;

Expand All @@ -179,7 +173,8 @@ public function findBy(array $criteria, array $orderBy = null, $limit = null, $o
*/
public function findOneBy(array $criteria)
{
return $this->findBy($criteria, null, 1);
$documents = $this->findBy($criteria, null, 1);
return $documents->isEmpty() ? null : $documents->first();
}

/**
Expand Down
Expand Up @@ -80,11 +80,35 @@ public function testFindBy()
$this->assertCount(1, $users3);

$users4 = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findBy(array('status' =>'active'), array('name'), 2, 0);
reset($users4);
$this->assertEquals('/functional/user1', key($users4));
$this->assertEquals('/functional/user1', $users4->key());

$users5 = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findBy(array('status' =>'active'), array('name'), 2, 1);
reset($users5);
$this->assertEquals('/functional/user2', key($users5));
$this->assertEquals('/functional/user2', $users5->key());
}

public function testFindOneBy()
{
$user1 = new \Doctrine\Tests\Models\CMS\CmsUser();
$user1->username = "beberlei";
$user1->status = "active";
$user1->name = "Benjamin";
$user1->id = '/functional/user1';

$user2 = new \Doctrine\Tests\Models\CMS\CmsUser();
$user2->username = "lsmith";
$user2->status = "active";
$user2->name = "Lukas";
$user2->id = '/functional/user2';

$this->dm->persist($user1);
$this->dm->persist($user2);
$this->dm->flush();

$users1 = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findOneBy(array('username' =>'beberlei'));
$this->assertEquals($user1->username, $users1->username);

$users2 = $this->dm->getRepository('Doctrine\Tests\Models\CMS\CmsUser')->findOneBy(array('username' =>'obama'));
$this->assertEquals(null, $users2);
}
}

0 comments on commit 85f8752

Please sign in to comment.