2014-09-26 20:41:28 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Dao;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Dao\EntityConverters\SnapshotEntityConverter;
|
|
|
|
use Szurubooru\Dao\UserDao;
|
|
|
|
use Szurubooru\DatabaseConnection;
|
|
|
|
use Szurubooru\Entities\Entity;
|
|
|
|
use Szurubooru\Entities\Snapshot;
|
2014-09-26 20:41:28 +02:00
|
|
|
|
|
|
|
class SnapshotDao extends AbstractDao
|
|
|
|
{
|
|
|
|
private $userDao;
|
|
|
|
|
|
|
|
public function __construct(
|
2014-10-08 14:47:47 +02:00
|
|
|
DatabaseConnection $databaseConnection,
|
|
|
|
UserDao $userDao)
|
2014-09-26 20:41:28 +02:00
|
|
|
{
|
|
|
|
parent::__construct(
|
|
|
|
$databaseConnection,
|
|
|
|
'snapshots',
|
2014-10-08 14:47:47 +02:00
|
|
|
new SnapshotEntityConverter());
|
2014-09-26 20:41:28 +02:00
|
|
|
|
|
|
|
$this->userDao = $userDao;
|
|
|
|
}
|
|
|
|
|
2014-11-18 21:22:46 +01:00
|
|
|
public function findEarlierSnapshots(Snapshot $snapshot)
|
2014-09-26 20:41:28 +02:00
|
|
|
{
|
2014-10-19 19:52:27 +02:00
|
|
|
$query = $this->pdo
|
2014-09-26 20:41:28 +02:00
|
|
|
->from($this->tableName)
|
2014-11-18 21:22:46 +01:00
|
|
|
->where('type', $snapshot->getType())
|
|
|
|
->where('primaryKey', $snapshot->getPrimaryKey())
|
2014-09-26 20:41:28 +02:00
|
|
|
->orderBy('time DESC');
|
2014-11-18 21:22:46 +01:00
|
|
|
|
|
|
|
if ($snapshot->getId())
|
|
|
|
$query->where('id < ?', $snapshot->getId());
|
|
|
|
|
2014-09-26 20:41:28 +02:00
|
|
|
return $this->arrayToEntities(iterator_to_array($query));
|
|
|
|
}
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
public function afterLoad(Entity $snapshot)
|
2014-09-26 20:41:28 +02:00
|
|
|
{
|
|
|
|
$snapshot->setLazyLoader(
|
2014-10-08 14:47:47 +02:00
|
|
|
Snapshot::LAZY_LOADER_USER,
|
|
|
|
function (Snapshot $snapshot)
|
2014-09-26 20:41:28 +02:00
|
|
|
{
|
|
|
|
return $this->getUser($snapshot);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
private function getUser(Snapshot $snapshot)
|
2014-09-26 20:41:28 +02:00
|
|
|
{
|
2014-11-19 19:07:29 +01:00
|
|
|
$userId = $snapshot->getUserId();
|
2014-09-26 20:41:28 +02:00
|
|
|
return $this->userDao->findById($userId);
|
|
|
|
}
|
|
|
|
}
|