-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTRepositoryQueryable.php
More file actions
45 lines (35 loc) · 1.3 KB
/
TRepositoryQueryable.php
File metadata and controls
45 lines (35 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php declare(strict_types = 1);
namespace Contributte\Nextras\Orm\QueryObject\Repository;
use Contributte\Nextras\Orm\QueryObject\Exception\InvalidHydrationModeException;
use Contributte\Nextras\Orm\QueryObject\ExecutableQueryObject;
use Contributte\Nextras\Orm\QueryObject\Queryable;
use Contributte\Nextras\Orm\QueryObject\QueryObject;
use Nextras\Dbal\Connection;
use Nextras\Dbal\Result\Result;
use Nextras\Orm\Collection\ICollection;
trait TRepositoryQueryable
{
protected Connection $connection;
public function injectConnection(Connection $connection): void
{
$this->connection = $connection;
}
public function fetch(QueryObject $queryObject, int $hydrationMode = Queryable::HYDRATION_RESULTSET): Result|ICollection
{
$qb = $queryObject->fetch($this->connection->createQueryBuilder());
if ($hydrationMode === Queryable::HYDRATION_RESULTSET) {
$result = $this->connection->queryArgs(
$qb->getQuerySql(),
$qb->getQueryParameters()
);
if ($queryObject instanceof ExecutableQueryObject) {
$result = $queryObject->postResult($result);
}
return $result;
} elseif ($hydrationMode === Queryable::HYDRATION_ENTITY) {
return $this->mapper->toCollection($qb);
} else {
throw new InvalidHydrationModeException(sprintf('Invalid hydration mode "%s"', $hydrationMode));
}
}
}