Added new method to get database records using StateInterface parameters.

This commit is contained in:
Abdulmhsen B. A. A
2022-05-17 21:22:10 +03:00
parent c3866e1111
commit a1484dca29
2 changed files with 34 additions and 10 deletions

View File

@@ -76,6 +76,16 @@ final class PDOAdapter implements StorageInterface
public function get(StateInterface $entity): StateInterface|null
{
if (null !== $entity->id) {
$stmt = $this->pdo->query("SELECT * FROM state WHERE id = " . (int)$entity->id);
$item = $stmt->fetch(PDO::FETCH_ASSOC);
if (true === is_array($item)) {
return $entity::fromArray($item);
}
}
if ($entity->isEpisode() && $entity->hasRelativeGuid() && null !== ($item = $this->findByRGuid($entity))) {
return $item;
}
@@ -108,6 +118,21 @@ final class PDOAdapter implements StorageInterface
return $arr;
}
public function find(StateInterface ...$items): array
{
$list = [];
foreach ($items as $item) {
if (null === ($entity = $this->get($item))) {
continue;
}
$list[$entity->id] = $entity;
}
return $list;
}
public function update(StateInterface $entity): StateInterface
{
try {
@@ -410,16 +435,6 @@ final class PDOAdapter implements StorageInterface
*/
private function findByGuid(StateInterface $entity): StateInterface|null
{
if (null !== $entity->id) {
$stmt = $this->pdo->query("SELECT * FROM state WHERE id = " . (int)$entity->id);
if (false === ($row = $stmt->fetch(PDO::FETCH_ASSOC))) {
return null;
}
return $entity::fromArray($row);
}
$guids = [];
$cond = [
'type' => $entity->type,

View File

@@ -46,6 +46,15 @@ interface StorageInterface
*/
public function getAll(DateTimeInterface|null $date = null, StateInterface|null $class = null): array;
/**
* Return database records for given items.
*
* @param array<StateInterface> $items
*
* @return array<StateInterface>
*/
public function find(StateInterface ...$items): array;
/**
* Update Entity immediately.
*