From 5cfb2254003b37b2e3e1e80d45cde642484b396c Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Thu, 18 Sep 2014 09:48:33 +0200 Subject: [PATCH] Fixed lazy loaders injection AbstractSearchService::getByFilter() wasn't injecting lazy loaders, because it didn't call AbstractDao::afterLoad(). This resulted in tags not showing up in post list, because there was nothing to retrieve them. Changed lazy loaders injection so that it's always executed as soon as possible (i.e. right in EntityConverter). --- src/Dao/AbstractDao.php | 14 ++++++------ .../AbstractEntityConverter.php | 22 +++++++++++++++++++ .../EntityConverters/PostEntityConverter.php | 4 ++-- .../EntityConverters/TagEntityConverter.php | 4 ++-- .../EntityConverters/TokenEntityConverter.php | 4 ++-- .../EntityConverters/UserEntityConverter.php | 4 ++-- src/Dao/Services/AbstractSearchService.php | 5 ++++- 7 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 src/Dao/EntityConverters/AbstractEntityConverter.php diff --git a/src/Dao/AbstractDao.php b/src/Dao/AbstractDao.php index b8e3dbed..6165704c 100644 --- a/src/Dao/AbstractDao.php +++ b/src/Dao/AbstractDao.php @@ -13,11 +13,14 @@ abstract class AbstractDao implements ICrudDao $tableName, \Szurubooru\Dao\EntityConverters\IEntityConverter $entityConverter) { - $this->tableName = $tableName; - $this->entityConverter = $entityConverter; - $this->pdo = $databaseConnection->getPDO(); $this->fpdo = new \FluentPDO($this->pdo); + $this->tableName = $tableName; + $this->entityConverter = $entityConverter; + $this->entityConverter->setEntityDecorator(function($entity) + { + $this->afterLoad($entity); + }); } public function getTableName() @@ -51,7 +54,6 @@ abstract class AbstractDao implements ICrudDao foreach ($query as $arrayEntity) { $entity = $this->entityConverter->toEntity($arrayEntity); - $this->afterLoad($entity); $entities[$entity->getId()] = $entity; } return $entities; @@ -98,9 +100,7 @@ abstract class AbstractDao implements ICrudDao if (!$arrayEntity) return null; - $entity = $this->entityConverter->toEntity($arrayEntity[0]); - $this->afterLoad($entity); - return $entity; + return $this->entityConverter->toEntity($arrayEntity[0]); } protected function deleteBy($columnName, $value) diff --git a/src/Dao/EntityConverters/AbstractEntityConverter.php b/src/Dao/EntityConverters/AbstractEntityConverter.php new file mode 100644 index 00000000..69591fae --- /dev/null +++ b/src/Dao/EntityConverters/AbstractEntityConverter.php @@ -0,0 +1,22 @@ +entityDecorator = $entityDecorator; + } + + public function toEntity(array $array) + { + $entity = $this->toBasicEntity($array); + if ($this->entityDecorator !== null) + call_user_func($this->entityDecorator, $entity); + return $entity; + } + + protected abstract function toBasicEntity(array $array); +} diff --git a/src/Dao/EntityConverters/PostEntityConverter.php b/src/Dao/EntityConverters/PostEntityConverter.php index 0a9353bf..4feebfde 100644 --- a/src/Dao/EntityConverters/PostEntityConverter.php +++ b/src/Dao/EntityConverters/PostEntityConverter.php @@ -1,7 +1,7 @@ setName($array['name']); diff --git a/src/Dao/EntityConverters/TagEntityConverter.php b/src/Dao/EntityConverters/TagEntityConverter.php index b5121240..65d569da 100644 --- a/src/Dao/EntityConverters/TagEntityConverter.php +++ b/src/Dao/EntityConverters/TagEntityConverter.php @@ -1,7 +1,7 @@ setName($array['name']); diff --git a/src/Dao/EntityConverters/TokenEntityConverter.php b/src/Dao/EntityConverters/TokenEntityConverter.php index 34d10f29..3b940299 100644 --- a/src/Dao/EntityConverters/TokenEntityConverter.php +++ b/src/Dao/EntityConverters/TokenEntityConverter.php @@ -1,7 +1,7 @@ setName($array['name']); diff --git a/src/Dao/EntityConverters/UserEntityConverter.php b/src/Dao/EntityConverters/UserEntityConverter.php index 1276f8cf..4534816d 100644 --- a/src/Dao/EntityConverters/UserEntityConverter.php +++ b/src/Dao/EntityConverters/UserEntityConverter.php @@ -1,7 +1,7 @@ setName($array['name']); diff --git a/src/Dao/Services/AbstractSearchService.php b/src/Dao/Services/AbstractSearchService.php index 795552c1..8aeb87ea 100644 --- a/src/Dao/Services/AbstractSearchService.php +++ b/src/Dao/Services/AbstractSearchService.php @@ -50,7 +50,10 @@ abstract class AbstractSearchService $entities = []; foreach ($query as $arrayEntity) - $entities[] = $this->entityConverter->toEntity($arrayEntity); + { + $entity = $this->entityConverter->toEntity($arrayEntity); + $entities[] = $entity; + } $query = $this->fpdo ->from($this->tableName)