szurubooru/src/Dao/AbstractDao.php

119 lines
2.6 KiB
PHP
Raw Normal View History

<?php
namespace Szurubooru\Dao;
abstract class AbstractDao implements ICrudDao
{
2014-09-14 16:16:15 +02:00
protected $pdo;
protected $fpdo;
protected $tableName;
2014-08-30 17:10:45 +02:00
protected $entityConverter;
2014-08-30 17:10:45 +02:00
public function __construct(
\Szurubooru\DatabaseConnection $databaseConnection,
2014-09-14 16:16:15 +02:00
$tableName,
\Szurubooru\Dao\EntityConverters\IEntityConverter $entityConverter)
{
2014-09-14 16:16:15 +02:00
$this->pdo = $databaseConnection->getPDO();
$this->fpdo = new \FluentPDO($this->pdo);
$this->tableName = $tableName;
$this->entityConverter = $entityConverter;
$this->entityConverter->setEntityDecorator(function($entity)
{
$this->afterLoad($entity);
});
}
2014-09-14 16:16:15 +02:00
public function getTableName()
{
2014-09-14 16:16:15 +02:00
return $this->tableName;
}
public function getEntityConverter()
{
return $this->entityConverter;
}
public function save(&$entity)
{
if ($entity->getId())
{
2014-09-15 11:38:24 +02:00
$entity = $this->update($entity);
}
else
{
2014-09-15 11:38:24 +02:00
$entity = $this->create($entity);
}
2014-09-15 11:38:24 +02:00
$this->afterSave($entity);
return $entity;
}
public function findAll()
{
$entities = [];
2014-09-14 16:16:15 +02:00
$query = $this->fpdo->from($this->tableName);
foreach ($query as $arrayEntity)
{
2014-08-30 17:10:45 +02:00
$entity = $this->entityConverter->toEntity($arrayEntity);
2014-09-14 16:16:15 +02:00
$entities[$entity->getId()] = $entity;
}
return $entities;
}
public function findById($entityId)
{
2014-09-14 16:16:15 +02:00
return $this->findOneBy('id', $entityId);
}
public function deleteAll()
{
2014-09-14 16:16:15 +02:00
$this->fpdo->deleteFrom($this->tableName)->execute();
}
public function deleteById($entityId)
{
2014-09-14 16:16:15 +02:00
return $this->deleteBy('id', $entityId);
}
2014-09-15 11:38:24 +02:00
protected function update(\Szurubooru\Entities\Entity $entity)
{
$arrayEntity = $this->entityConverter->toArray($entity);
$this->fpdo->update($this->tableName)->set($arrayEntity)->where('id', $entity->getId())->execute();
return $entity;
}
protected function create(\Szurubooru\Entities\Entity $entity)
{
$arrayEntity = $this->entityConverter->toArray($entity);
$this->fpdo->insertInto($this->tableName)->values($arrayEntity)->execute();
$entity->setId(intval($this->pdo->lastInsertId()));
return $entity;
}
2014-09-14 16:16:15 +02:00
protected function hasAnyRecords()
{
return count(iterator_to_array($this->fpdo->from($this->tableName)->limit(1))) > 0;
}
protected function findOneBy($columnName, $value)
{
$arrayEntity = iterator_to_array($this->fpdo->from($this->tableName)->where($columnName, $value));
2014-09-15 11:38:24 +02:00
if (!$arrayEntity)
return null;
return $this->entityConverter->toEntity($arrayEntity[0]);
2014-09-14 16:16:15 +02:00
}
protected function deleteBy($columnName, $value)
{
$this->fpdo->deleteFrom($this->tableName)->where($columnName, $value)->execute();
}
2014-09-15 11:38:24 +02:00
protected function afterLoad(\Szurubooru\Entities\Entity $entity)
{
}
protected function afterSave(\Szurubooru\Entities\Entity $entity)
{
}
}