Last active
July 7, 2017 22:56
-
-
Save MacDada/025ef645837363fef0ec1ea0a2ead6fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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