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)