|
@@ -37,9 +37,11 @@ final class QueryBuilder
|
|
|
|
|
|
|
|
private array $joins = [];
|
|
private array $joins = [];
|
|
|
private array $joinsAlreadyAdded = [];
|
|
private array $joinsAlreadyAdded = [];
|
|
|
-
|
|
|
|
|
|
|
+ private ?int $firstResult = null;
|
|
|
private ?int $maxResults = null;
|
|
private ?int $maxResults = null;
|
|
|
|
|
|
|
|
|
|
+ private array $params = [];
|
|
|
|
|
+
|
|
|
public function __construct(EntityManagerInterface $em, string $primaryKey = 'id')
|
|
public function __construct(EntityManagerInterface $em, string $primaryKey = 'id')
|
|
|
{
|
|
{
|
|
|
$this->platform = $em->getPlatform();
|
|
$this->platform = $em->getPlatform();
|
|
@@ -49,27 +51,32 @@ final class QueryBuilder
|
|
|
$this->primaryKey = $primaryKey;
|
|
$this->primaryKey = $primaryKey;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function getResultIterator(array $parameters = [], string $hydrationMode = self::HYDRATE_OBJECT): iterable
|
|
|
|
|
|
|
+ public function getResultIterator(string $hydrationMode = self::HYDRATE_OBJECT): iterable
|
|
|
{
|
|
{
|
|
|
- foreach ($this->buildSqlQuery()->getResultIterator($parameters) as $item) {
|
|
|
|
|
|
|
+ foreach ($this->buildSqlQuery()->getResultIterator() as $item) {
|
|
|
yield $this->hydrate([$item], $hydrationMode)[0];
|
|
yield $this->hydrate([$item], $hydrationMode)[0];
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function getResult(array $parameters = [], string $hydrationMode = self::HYDRATE_OBJECT): array
|
|
|
|
|
|
|
+ public function getResult(string $hydrationMode = self::HYDRATE_OBJECT): array
|
|
|
{
|
|
{
|
|
|
- return $this->hydrate($this->buildSqlQuery()->getResult($parameters), $hydrationMode);
|
|
|
|
|
|
|
+ return $this->hydrate($this->buildSqlQuery()->getResult(), $hydrationMode);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function getOneOrNullResult(array $parameters = [], string $hydrationMode = self::HYDRATE_OBJECT)
|
|
|
|
|
|
|
+ public function getOneOrNullResult(string $hydrationMode = self::HYDRATE_OBJECT)
|
|
|
{
|
|
{
|
|
|
- $item = $this->buildSqlQuery()->getOneOrNullResult($parameters);
|
|
|
|
|
|
|
+ $item = $this->buildSqlQuery()->getOneOrNullResult();
|
|
|
if ($item === null) {
|
|
if ($item === null) {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
return $this->hydrate([$item], $hydrationMode)[0];
|
|
return $this->hydrate([$item], $hydrationMode)[0];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public function getCountResult(): int
|
|
|
|
|
+ {
|
|
|
|
|
+ return $this->buildSqlQuery()->count();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public function select(string $entityName, array $properties = []): self
|
|
public function select(string $entityName, array $properties = []): self
|
|
|
{
|
|
{
|
|
|
$this->select = [
|
|
$this->select = [
|
|
@@ -115,6 +122,18 @@ final class QueryBuilder
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public function setParams(array $params): self
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->params = $params;
|
|
|
|
|
+ return $this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public function setParam(string $name, $value): self
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->params[$name] = $value;
|
|
|
|
|
+ return $this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public function resetWhere(): self
|
|
public function resetWhere(): self
|
|
|
{
|
|
{
|
|
|
$this->where = [];
|
|
$this->where = [];
|
|
@@ -127,6 +146,12 @@ final class QueryBuilder
|
|
|
return $this;
|
|
return $this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public function resetParams(): self
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->params = [];
|
|
|
|
|
+ return $this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public function leftJoin(string $fromAliasOrEntityName, string $targetEntityName, ?string $property = null): self
|
|
public function leftJoin(string $fromAliasOrEntityName, string $targetEntityName, ?string $property = null): self
|
|
|
{
|
|
{
|
|
|
return $this->join('LEFT', $fromAliasOrEntityName, $targetEntityName, $property);
|
|
return $this->join('LEFT', $fromAliasOrEntityName, $targetEntityName, $property);
|
|
@@ -137,6 +162,17 @@ final class QueryBuilder
|
|
|
return $this->join('INNER', $fromAliasOrEntityName, $targetEntityName, $property);
|
|
return $this->join('INNER', $fromAliasOrEntityName, $targetEntityName, $property);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public function setFirstResult(?int $firstResult): self
|
|
|
|
|
+ {
|
|
|
|
|
+ if ($this->select === []) {
|
|
|
|
|
+ throw new LogicException(
|
|
|
|
|
+ 'You must call the select() method first to define the main table for the query '
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ $this->firstResult = $firstResult;
|
|
|
|
|
+ return $this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public function setMaxResults(?int $maxResults): self
|
|
public function setMaxResults(?int $maxResults): self
|
|
|
{
|
|
{
|
|
|
if ($this->select === []) {
|
|
if ($this->select === []) {
|
|
@@ -332,9 +368,16 @@ final class QueryBuilder
|
|
|
$joinQl->orderBy($this->resolveExpression($orderBy['sort']), $orderBy['order']);
|
|
$joinQl->orderBy($this->resolveExpression($orderBy['sort']), $orderBy['order']);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ foreach ($this->params as $key => $value) {
|
|
|
|
|
+ $joinQl->setParam($key, $value);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if ($this->maxResults) {
|
|
if ($this->maxResults) {
|
|
|
$joinQl->setMaxResults($this->maxResults);
|
|
$joinQl->setMaxResults($this->maxResults);
|
|
|
}
|
|
}
|
|
|
|
|
+ if ($this->firstResult) {
|
|
|
|
|
+ $joinQl->setFirstResult($this->firstResult);
|
|
|
|
|
+ }
|
|
|
return $joinQl;
|
|
return $joinQl;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -378,12 +421,12 @@ final class QueryBuilder
|
|
|
|
|
|
|
|
private function hydrate(array $data, string $hydrationMode): array
|
|
private function hydrate(array $data, string $hydrationMode): array
|
|
|
{
|
|
{
|
|
|
- if ($hydrationMode === self::HYDRATE_ARRAY) {
|
|
|
|
|
- $hydrator = new ArrayHydrator();
|
|
|
|
|
|
|
+ if ($hydrationMode === self::HYDRATE_OBJECT) {
|
|
|
|
|
+ $hydrator = new EntityHydrator($this->cache);
|
|
|
} elseif ($hydrationMode === self::HYDRATE_OBJECT_READONLY) {
|
|
} elseif ($hydrationMode === self::HYDRATE_OBJECT_READONLY) {
|
|
|
$hydrator = new ReadOnlyEntityHydrator();
|
|
$hydrator = new ReadOnlyEntityHydrator();
|
|
|
} else {
|
|
} else {
|
|
|
- $hydrator = new EntityHydrator($this->cache);
|
|
|
|
|
|
|
+ $hydrator = new ArrayHydrator();
|
|
|
}
|
|
}
|
|
|
$collection = [];
|
|
$collection = [];
|
|
|
foreach ($data as $item) {
|
|
foreach ($data as $item) {
|