title: How to implement DDD with Doctrine2 within a Symfony2 project?
body: <p>Well I'm trying to introduce myself to DDD, I'm very new to it and some concepts are still unclear.</p>
  
  <p>Here is what I understood so far:</p>
  
  <ul>
  <li>The domain is basically about data</li>
  <li>The persistence layer is not tied to the domain, but business logic transaction may be.</li>
  </ul>
  
  <p>When using Doctrine2, we use either EntityRepository or a CustomEntityRepository implementation.</p>
  
  <p>In DDD, the Repository pattern seems a bit different, I've looked at .NET &amp; Java examples as well as messages from the DDD mailing list, and people tends to accord that the repository should returns QueryObject, in Doctrine2, I project to return QueryBuilder instance from my repository.</p>
  
  <p>Therefore to hide the complexity of working with QueryBuilder then a Query then an Hydrated result set, I implemented another service layer which I called Manager.</p>
  
  <p>Here is how my domain looks like:</p>
  
  <pre><code>src/Domain/
  ├── Entity
  │   ├── AbstractComment.php
  │   ├── Comment.php
  ├── Manager
  │   ├── CommentManager.php
  └── Repository
      └── CommentRepository.php
  </code></pre>
  
  <p>The Entity folder is just about pure POPO.</p>
  
  <p>The <code>CommentRepository</code> looks like this:</p>
  
  <pre><code>&lt;?php
  namespace Acme\Domain\Repository;
  
  use Doctrine\Common\Collections\Criteria;
  
  class CommentRepository
  {
      private $em;
  
      public function __construct(EntityManager $em)
      {
          $this-&gt;em = $em;
      }
  
      /**
       * @param $id
       *
       * @return \Doctrine\ORM\QueryBuilder
       */
      public function findOneById($id)
      {
          $qb = $this-&gt;getEntityManager()
              -&gt;getRepository('Acme:Domain\Entity\Comment')
                  -&gt;createQueryBuilder('c');
  
          $criteria = new Criteria();
  
          $criteria-&gt;andWhere(
              $criteria-&gt;expr()-&gt;eq('c.id', ':id')
          );
  
          $qb-&gt;addCriteria($criteria);
  
          $qb-&gt;setParameter('id', $id);
  
          return $qb;
      }
  }
  </code></pre>
  
  <p>And the <code>CommentManager</code>:</p>
  
  <pre><code>&lt;?php
  
  namespace Acme\Domain\Manager;
  
  class CommentManager
  {
      protected $repository;
  
      public function __construct(CommentRepository $repository)
      {
          $this-&gt;repository = $repository;
      }
  
      public function findOneById($id)
      {
          return $this-&gt;repository-&gt;findOneById($id)-&gt;getQuery()-&gt;getOneOrNullResult();
      }
  }
  </code></pre>
  
  <ol>
  <li>Is it the correct approach for managing "entities"?</li>
  <li>Following such pattern, where do I have to handle the persistence? </li>
  </ol>
  
  <p>I mean, if I'm right, the repository is basically like a collection, therefore it should provide <code>add(Entity $e)</code> and <code>remove(Entity $e)</code> methods, but where do I actually persist the entity?</p>
  
  <p>Is it safe to do it inside the <code>add()</code> &amp; <code>remove()</code> methods? Is it better to add a <code>save()</code> method to handle updates?</p>
  
  <p>Thank you for your time.</p>