2014-08-28 10:45:55 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Dao;
|
|
|
|
|
|
|
|
abstract class AbstractDao implements ICrudDao
|
|
|
|
{
|
|
|
|
protected $db;
|
|
|
|
protected $collection;
|
|
|
|
protected $entityName;
|
2014-08-30 17:10:45 +02:00
|
|
|
protected $entityConverter;
|
2014-08-28 10:45:55 +02:00
|
|
|
|
2014-08-30 17:10:45 +02:00
|
|
|
public function __construct(
|
|
|
|
\Szurubooru\DatabaseConnection $databaseConnection,
|
|
|
|
$collectionName,
|
|
|
|
$entityName)
|
2014-08-28 10:45:55 +02:00
|
|
|
{
|
2014-08-30 17:10:45 +02:00
|
|
|
$this->entityConverter = new EntityConverter($entityName);
|
|
|
|
$this->db = $databaseConnection->getDatabase();
|
2014-08-30 12:07:34 +02:00
|
|
|
$this->collection = $this->db->selectCollection($collectionName);
|
2014-08-28 10:45:55 +02:00
|
|
|
$this->entityName = $entityName;
|
|
|
|
}
|
|
|
|
|
2014-09-03 19:07:53 +02:00
|
|
|
public function getCollection()
|
|
|
|
{
|
|
|
|
return $this->collection;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEntityConverter()
|
|
|
|
{
|
|
|
|
return $this->entityConverter;
|
|
|
|
}
|
|
|
|
|
2014-08-28 10:45:55 +02:00
|
|
|
public function save(&$entity)
|
|
|
|
{
|
2014-08-30 17:10:45 +02:00
|
|
|
$arrayEntity = $this->entityConverter->toArray($entity);
|
2014-09-13 23:58:13 +02:00
|
|
|
if ($entity->getId())
|
2014-08-28 10:45:55 +02:00
|
|
|
{
|
2014-09-10 19:16:06 +02:00
|
|
|
$savedId = $arrayEntity['_id'];
|
|
|
|
unset($arrayEntity['_id']);
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->collection->update(['_id' => new \MongoId($entity->getId())], $arrayEntity, ['w' => true]);
|
2014-09-10 19:16:06 +02:00
|
|
|
$arrayEntity['_id'] = $savedId;
|
2014-08-28 10:45:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-31 16:55:49 +02:00
|
|
|
$this->collection->insert($arrayEntity, ['w' => true]);
|
2014-08-28 10:45:55 +02:00
|
|
|
}
|
2014-08-30 17:10:45 +02:00
|
|
|
$entity = $this->entityConverter->toEntity($arrayEntity);
|
2014-08-28 10:45:55 +02:00
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
|
2014-09-13 23:58:13 +02:00
|
|
|
public function findAll()
|
2014-08-28 10:45:55 +02:00
|
|
|
{
|
|
|
|
$entities = [];
|
|
|
|
foreach ($this->collection->find() as $key => $arrayEntity)
|
|
|
|
{
|
2014-08-30 17:10:45 +02:00
|
|
|
$entity = $this->entityConverter->toEntity($arrayEntity);
|
2014-08-28 10:45:55 +02:00
|
|
|
$entities[$key] = $entity;
|
|
|
|
}
|
|
|
|
return $entities;
|
|
|
|
}
|
|
|
|
|
2014-09-13 23:58:13 +02:00
|
|
|
public function findById($entityId)
|
2014-08-28 10:45:55 +02:00
|
|
|
{
|
2014-09-13 23:58:13 +02:00
|
|
|
$arrayEntity = $this->collection->findOne(['_id' => new \MongoId($entityId)]);
|
2014-08-30 17:10:45 +02:00
|
|
|
return $this->entityConverter->toEntity($arrayEntity);
|
2014-08-28 10:45:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteAll()
|
|
|
|
{
|
|
|
|
$this->collection->remove();
|
|
|
|
}
|
|
|
|
|
2014-09-13 23:58:13 +02:00
|
|
|
public function deleteById($entityId)
|
2014-08-28 10:45:55 +02:00
|
|
|
{
|
2014-09-13 23:58:13 +02:00
|
|
|
$this->collection->remove(['_id' => new \MongoId($entityId)]);
|
2014-08-28 10:45:55 +02:00
|
|
|
}
|
|
|
|
}
|