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).
This commit is contained in:
Marcin Kurczewski 2014-09-18 09:48:33 +02:00
parent eb7c507359
commit 5cfb225400
7 changed files with 41 additions and 16 deletions

View file

@ -13,11 +13,14 @@ abstract class AbstractDao implements ICrudDao
$tableName, $tableName,
\Szurubooru\Dao\EntityConverters\IEntityConverter $entityConverter) \Szurubooru\Dao\EntityConverters\IEntityConverter $entityConverter)
{ {
$this->tableName = $tableName;
$this->entityConverter = $entityConverter;
$this->pdo = $databaseConnection->getPDO(); $this->pdo = $databaseConnection->getPDO();
$this->fpdo = new \FluentPDO($this->pdo); $this->fpdo = new \FluentPDO($this->pdo);
$this->tableName = $tableName;
$this->entityConverter = $entityConverter;
$this->entityConverter->setEntityDecorator(function($entity)
{
$this->afterLoad($entity);
});
} }
public function getTableName() public function getTableName()
@ -51,7 +54,6 @@ abstract class AbstractDao implements ICrudDao
foreach ($query as $arrayEntity) foreach ($query as $arrayEntity)
{ {
$entity = $this->entityConverter->toEntity($arrayEntity); $entity = $this->entityConverter->toEntity($arrayEntity);
$this->afterLoad($entity);
$entities[$entity->getId()] = $entity; $entities[$entity->getId()] = $entity;
} }
return $entities; return $entities;
@ -98,9 +100,7 @@ abstract class AbstractDao implements ICrudDao
if (!$arrayEntity) if (!$arrayEntity)
return null; return null;
$entity = $this->entityConverter->toEntity($arrayEntity[0]); return $this->entityConverter->toEntity($arrayEntity[0]);
$this->afterLoad($entity);
return $entity;
} }
protected function deleteBy($columnName, $value) protected function deleteBy($columnName, $value)

View file

@ -0,0 +1,22 @@
<?php
namespace Szurubooru\Dao\EntityConverters;
abstract class AbstractEntityConverter implements IEntityConverter
{
private $entityDecorator = null;
public function setEntityDecorator($entityDecorator)
{
$this->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);
}

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Szurubooru\Dao\EntityConverters; namespace Szurubooru\Dao\EntityConverters;
class PostEntityConverter implements IEntityConverter class PostEntityConverter extends AbstractEntityConverter implements IEntityConverter
{ {
public function toArray(\Szurubooru\Entities\Entity $entity) public function toArray(\Szurubooru\Entities\Entity $entity)
{ {
@ -23,7 +23,7 @@ class PostEntityConverter implements IEntityConverter
]; ];
} }
public function toEntity(array $array) public function toBasicEntity(array $array)
{ {
$entity = new \Szurubooru\Entities\Post(intval($array['id'])); $entity = new \Szurubooru\Entities\Post(intval($array['id']));
$entity->setName($array['name']); $entity->setName($array['name']);

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Szurubooru\Dao\EntityConverters; namespace Szurubooru\Dao\EntityConverters;
class TagEntityConverter implements IEntityConverter class TagEntityConverter extends AbstractEntityConverter implements IEntityConverter
{ {
public function toArray(\Szurubooru\Entities\Entity $entity) public function toArray(\Szurubooru\Entities\Entity $entity)
{ {
@ -11,7 +11,7 @@ class TagEntityConverter implements IEntityConverter
]; ];
} }
public function toEntity(array $array) public function toBasicEntity(array $array)
{ {
$entity = new \Szurubooru\Entities\Tag($array['name']); $entity = new \Szurubooru\Entities\Tag($array['name']);
$entity->setName($array['name']); $entity->setName($array['name']);

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Szurubooru\Dao\EntityConverters; namespace Szurubooru\Dao\EntityConverters;
class TokenEntityConverter implements IEntityConverter class TokenEntityConverter extends AbstractEntityConverter implements IEntityConverter
{ {
public function toArray(\Szurubooru\Entities\Entity $entity) public function toArray(\Szurubooru\Entities\Entity $entity)
{ {
@ -14,7 +14,7 @@ class TokenEntityConverter implements IEntityConverter
]; ];
} }
public function toEntity(array $array) public function toBasicEntity(array $array)
{ {
$entity = new \Szurubooru\Entities\Token(intval($array['id'])); $entity = new \Szurubooru\Entities\Token(intval($array['id']));
$entity->setName($array['name']); $entity->setName($array['name']);

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Szurubooru\Dao\EntityConverters; namespace Szurubooru\Dao\EntityConverters;
class UserEntityConverter implements IEntityConverter class UserEntityConverter extends AbstractEntityConverter implements IEntityConverter
{ {
public function toArray(\Szurubooru\Entities\Entity $entity) public function toArray(\Szurubooru\Entities\Entity $entity)
{ {
@ -21,7 +21,7 @@ class UserEntityConverter implements IEntityConverter
]; ];
} }
public function toEntity(array $array) public function toBasicEntity(array $array)
{ {
$entity = new \Szurubooru\Entities\User(intval($array['id'])); $entity = new \Szurubooru\Entities\User(intval($array['id']));
$entity->setName($array['name']); $entity->setName($array['name']);

View file

@ -50,7 +50,10 @@ abstract class AbstractSearchService
$entities = []; $entities = [];
foreach ($query as $arrayEntity) foreach ($query as $arrayEntity)
$entities[] = $this->entityConverter->toEntity($arrayEntity); {
$entity = $this->entityConverter->toEntity($arrayEntity);
$entities[] = $entity;
}
$query = $this->fpdo $query = $this->fpdo
->from($this->tableName) ->from($this->tableName)