Skip to content

Instantly share code, notes, and snippets.

@MacDada
Last active July 7, 2017 22:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MacDada/025ef645837363fef0ec1ea0a2ead6fb to your computer and use it in GitHub Desktop.
Save MacDada/025ef645837363fef0ec1ea0a2ead6fb to your computer and use it in GitHub Desktop.
<?php
/**
* Simpler version of
* https://github.com/dddinphp/repository-examples/blob/master/src/Infrastructure/Persistence/Doctrine/DoctrinePostRepository.php#L64
*
* Thanks to using inheritance.
*/
namespace Infrastructure\Persistence\Doctrine;
use Doctrine\ORM\EntityRepository;
use Domain\Model\Post;
use Domain\Model\PostId;
use Domain\Model\CollectionPostRepository as PostRepository;
class DoctrinePostRepository extends EntityRepository implements PostRepository
{
public function add(Post $aPost)
{
$this->_em->persist($aPost);
}
public function remove(Post $aPost)
{
$this->_em->remove($aPost);
}
public function postOfId(PostId $anId)
{
return $this->findOneByPostId($anId);
}
public function latestPosts(\DateTime $sinceADate)
{
return $this->em->createQueryBuilder('p')
->where('p.createdAt > :since')
->setParameter(':since', $sinceADate)
->getQuery()
->getResult();
}
/**
* @param DoctrinePostSpecification $specification
*
* @return Post[]
*/
public function query($specification)
{
return $specification->buildQuery($this->_em)->getResult();
}
public function nextIdentity()
{
return new PostId();
}
public function size()
{
return $this->_em->createQueryBuilder('p')
->select('count(p.id)')
->getQuery()
->getSingleScalarResult();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment