DQL Implementation

In the case of this Repository, we'll only need the EntityManager to retrieve Domain objects directly from the database:

namespace InfrastructurePersistenceDoctrine;

use DoctrineORMEntityManager;
use DomainModelPost;
use DomainModelPostId;
use DomainModelPostRepository;

class DoctrinePostRepository implements PostRepository
{
protected $em;

public function __construct(EntityManager $em)
{
$this->em = $em;
}
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->em->find('DomainModelPost', $anId);
}

public function latestPosts(DateTimeImmutable $sinceADate)
{
return $this->em->createQueryBuilder()
->select('p')
->from('DomainModelPost', 'p')
->where('p.createdAt > :since')
->setParameter(':since', $sinceADate)
->getQuery()
->getResult();
}

public function nextIdentity()
{
return new PostId();
}
}

If you check some Doctrine examples out there, you may find that after running persist or remove, flush should be called. But as seen in our proposal, there's no call to flush. Flushing and dealing with transactions is delegated to the Application Service. That's why you can work with Doctrine, considering that flushing all the changes on Entities will happen at the end of the request. In terms of performance, one flush call is best.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset