title: Doctrine: Can I flush only one class of entities?
body: <p>I like the general idea of passing Doctrine repositories as services in Symfony2 and avoiding passing EntityManager. However, while it's fine when reading data, the saving logic becomes a bit problematic here.</p>
  
  <p>Let's take this as a reference: <a href="http://php-and-symfony.matthiasnoback.nl/2014/05/inject-a-repository-instead-of-an-entity-manager/" rel="noreferrer">http://php-and-symfony.matthiasnoback.nl/2014/05/inject-a-repository-instead-of-an-entity-manager/</a>, but with a change separating persisting and flushing:</p>
  
  <pre><code>class DoctrineORMCustomerRepository extends EntityRepository implements CustomerRepository
  {
      public function persist(Customer $customer)
      {
          $this-&gt;_em-&gt;persist($customer);
      }
  
      public function flush()
      {
          $this-&gt;_em-&gt;flush();
      }
  }
  </code></pre>
  
  <p>The problem is you flush in a particular repository all changes in all entities.</p>
  
  <p>Now, is it possible to flush just one class of entities? (possibly cascading to dependent entities), so I could basically do something like:</p>
  
  <pre><code>foreach ($customers as $customer) {
      $this-&gt;customerRepository-&gt;persist($customer);
  }
  
  $this-&gt;customerRepository-&gt;flush();
  </code></pre>
  
  <p>I considered something like:</p>
  
  <pre><code>$this-&gt;_em-&gt;flush(getUnitOfWork()-&gt;getIdentityMap()[$this-&gt;_entityName]);
  </code></pre>
  
  <p>But I must have misunderstood something, because it doesn't work.</p>
  
  <p>EDIT: Yes, I'm aware I can do <code>$this-&gt;_em-&gt;flush($entity)</code>, but doing this one by one is suboptimal. I even know I can do <code>$this-&gt;_em-&gt;flush($arrayOfEntities)</code>, but to make the "foreach" example work this way I'd have to keep track of all the persisted entities in the repository duplicating some Doctrine internals.</p>