-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
namespace Librette\Doctrine\Queries; | ||
|
||
/** | ||
* @author David Matejka | ||
*/ | ||
class SelectOneQuery extends BaseQueryObject | ||
{ | ||
|
||
/** @var string */ | ||
private $entityClass; | ||
|
||
/** @var array */ | ||
private $filters = []; | ||
|
||
/** @var array */ | ||
private $orderBy = []; | ||
|
||
/** | ||
* @param string | ||
*/ | ||
public function __construct($entityClass, array $filters = []) | ||
{ | ||
$this->entityClass = $entityClass; | ||
$this->filters = array_map(NULL, array_keys($filters), $filters); | ||
} | ||
|
||
|
||
/** | ||
* @param string|\Closure | ||
* @param string|array|null|mixed | ||
* @return self | ||
*/ | ||
public function filterBy($field, $value = NULL) | ||
{ | ||
$this->filters[] = [$field, $value]; | ||
|
||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* @param string | ||
* @param string | ||
* @return self | ||
*/ | ||
public function orderBy($field, $direction = 'ASC') | ||
{ | ||
$this->orderBy[$field] = $direction; | ||
|
||
return $this; | ||
} | ||
|
||
|
||
protected function doFetch(Queryable $queryable) | ||
{ | ||
$qb = $queryable->createQueryBuilder($this->entityClass, 'e'); | ||
foreach ($this->filters as $filter) { | ||
list ($field, $value) = $filter; | ||
if ($value === NULL && $field instanceof \Closure) { | ||
$field($qb, 'e'); | ||
} else { | ||
$qb->whereCriteria([$field => $value]); | ||
} | ||
} | ||
foreach ($this->orderBy as $field => $direction) { | ||
$qb->autoJoinOrderBy($field, $direction); | ||
} | ||
$qb->setMaxResults(1); | ||
|
||
return $qb->getQuery()->getOneOrNullResult(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
namespace LibretteTests\Doctrine\Queries; | ||
|
||
use Doctrine\DBAL\Logging\DebugStack; | ||
use Librette\Doctrine\Queries\EntityQuery; | ||
use Librette\Doctrine\Queries\Queryable; | ||
use Librette\Doctrine\Queries\QueryHandler; | ||
use Librette\Doctrine\Queries\SelectOneQuery; | ||
use Librette\Queries\IQueryHandlerAccessor; | ||
use LibretteTests\Doctrine\Queries\Model\User; | ||
use Nette; | ||
use Tester; | ||
use Tester\Assert; | ||
|
||
require_once __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
/** | ||
* @author David Matějka | ||
* @testCase | ||
*/ | ||
class SelectOneQueryTestCase extends Tester\TestCase | ||
{ | ||
use EntityManagerTest; | ||
|
||
|
||
public function setUp() | ||
{ | ||
} | ||
|
||
|
||
public function testSelect() | ||
{ | ||
$em = $this->createMemoryManager(); | ||
$queryHandler = new QueryHandler(new Queryable($em, \Mockery::mock(IQueryHandlerAccessor::class))); | ||
$em->persist($john = new User('John')); | ||
$em->persist($jack = new User('Jack')); | ||
$em->flush(); | ||
$query = new SelectOneQuery(User::class, ['name' => 'John']); | ||
Assert::same($john, $queryHandler->fetch($query)); | ||
$query = new SelectOneQuery(User::class, ['name' => 'Jack']); | ||
Assert::same($jack, $queryHandler->fetch($query)); | ||
$query = new SelectOneQuery(User::class, ['name' => 'Jane']); | ||
Assert::null($queryHandler->fetch($query)); | ||
} | ||
|
||
} | ||
|
||
|
||
\run(new SelectOneQueryTestCase()); |