Skip to content

Commit 1faf615

Browse files
committed
Add possibility to use Where class in _findOne internal method
1 parent 9a26bbe commit 1faf615

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

src/AbstractTable.php

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,12 @@ protected function _findByPk(mixed $pk): ?array
199199
}
200200

201201
/**
202-
* @param array<string, mixed> $where
202+
* @param array<string, mixed>|Where $where
203203
* @param array<string, string>|string $orderBy
204204
* @return array<string, mixed>|null
205205
* @throws \Doctrine\DBAL\Exception
206206
*/
207-
protected function _findOne(array $where, array|string $orderBy = []): ?array
207+
protected function _findOne(array|Where $where, array|string $orderBy = []): ?array
208208
{
209209
$query = $this->select();
210210
$this->buildWhere($query, $where);
@@ -241,14 +241,7 @@ protected function _findAll(
241241
): array
242242
{
243243
$query = $this->select();
244-
if (is_array($where)) {
245-
$this->buildWhere($query, $where);
246-
} else {
247-
$query->where($where->string);
248-
foreach ($where->params as $param => $value) {
249-
$query->setParameter($param, $value);
250-
}
251-
}
244+
$this->buildWhere($query, $where);
252245
$this->applyOrderBy($query, $orderBy);
253246
if ($limit > 0) {
254247
$query->setMaxResults($limit);
@@ -332,40 +325,47 @@ protected function select(string $select = '*'): QueryBuilder
332325
}
333326

334327
/**
335-
* @param array<string, mixed> $where
328+
* @param array<string, mixed>|Where $where
336329
*/
337-
private function buildWhere(QueryBuilder $query, array $where): void
330+
private function buildWhere(QueryBuilder $query, array|Where $where): void
338331
{
339-
foreach ($where as $column => $value) {
340-
if ($value instanceof \BackedEnum) {
341-
$value = $value->value;
342-
} elseif ($value instanceof \UnitEnum) {
343-
$value = $value->name;
344-
}
332+
if (is_array($where)) {
333+
foreach ($where as $column => $value) {
334+
if ($value instanceof \BackedEnum) {
335+
$value = $value->value;
336+
} elseif ($value instanceof \UnitEnum) {
337+
$value = $value->name;
338+
}
345339

346-
if (is_null($value)) {
347-
$query->andWhere($column . ' IS NULL');
348-
} elseif (is_array($value) && count($value) === 2 && \in_array($value[0], self::COMPARISON_SIGNS)) {
349-
$comparisonSign = $value[0];
350-
$comparisonValue = $value[1];
340+
if (is_null($value)) {
341+
$query->andWhere($column . ' IS NULL');
342+
} elseif (is_array($value) && count($value) === 2 && \in_array($value[0], self::COMPARISON_SIGNS)) {
343+
$comparisonSign = $value[0];
344+
$comparisonValue = $value[1];
351345

352-
// Handle special case of "!= null"
353-
if ($comparisonSign === '!=' && is_null($comparisonValue)) {
354-
$query->andWhere($column . ' IS NOT NULL');
346+
// Handle special case of "!= null"
347+
if ($comparisonSign === '!=' && is_null($comparisonValue)) {
348+
$query->andWhere($column . ' IS NOT NULL');
349+
} else {
350+
$query->andWhere($column . ' ' . $comparisonSign . ' :' . $column)
351+
->setParameter($column, $comparisonValue);
352+
}
353+
} elseif (is_array($value)) {
354+
$placeholders = [];
355+
foreach ($value as $index => $val) {
356+
$placeholders[] = ':' . $column . $index;
357+
$query->setParameter($column . $index, $val);
358+
}
359+
$query->andWhere($column . ' IN(' . implode(', ', $placeholders) . ')');
355360
} else {
356-
$query->andWhere($column . ' ' . $comparisonSign . ' :' . $column)
357-
->setParameter($column, $comparisonValue);
358-
}
359-
} elseif (is_array($value)) {
360-
$placeholders = [];
361-
foreach ($value as $index => $val) {
362-
$placeholders[] = ':' . $column . $index;
363-
$query->setParameter($column . $index, $val);
361+
$query->andWhere($column . ' = :' . $column)
362+
->setParameter($column, $value);
364363
}
365-
$query->andWhere($column . ' IN(' . implode(', ', $placeholders) . ')');
366-
} else {
367-
$query->andWhere($column . ' = :' . $column)
368-
->setParameter($column, $value);
364+
}
365+
} else {
366+
$query->where($where->string);
367+
foreach ($where->params as $param => $value) {
368+
$query->setParameter($param, $value);
369369
}
370370
}
371371
}

0 commit comments

Comments
 (0)