title: Alice Fixture: Reference to entity already persisted & flush
body: <p>I simplified my problem with an exemple of a theoretical <code>Article</code> entity.
  My Article has a title, content category and a type</p>
  
  <p>My fixtures are writed in separates YAML files</p>
  
  <pre><code># category.yml
  AppBundle\Entity\Category:
      category_1:
          id: 1
          name: foo
      category_2:
          id: 2
          name: bar
  
  
  # type.yml
  AppBundle\Entity\Type:
      type_1:
          id: 1
          name: baz
      type_2:
          id: 2
          name: qux
  
  # article.yml
  AppBundle\Entity\Article:
      article_1:
          id: 1
          title: "Lorem ipsum dolor sit amet, consectetur"
          content: "Lorem ipsum dolor sit amet, consectetur"
          category: '@category_1'
  </code></pre>
  
  <p>The problem is I have a DoctrineListener on <code>Events::preUpdate</code> which update Article::type depends of Article::Category</p>
  
  <pre><code>/**
  * Doctrine Listener
  */
  class ArticleListener
  {
     // ...
      private function preUpdate(LifecycleEventArgs $args)
      {
          $article = $args-&gt;getEntity();
  
          // Get type from DB
          $type = $em-&gt;getRepository('AppBundle:Type')-&gt;find(1)
  
          $article-&gt;setType($type);
      }
  }
  </code></pre>
  
  <p>So load category &amp; type fixtures first</p>
  
  <pre><code>class BasicFixtures extends Fixture
  {
      public function load(ObjectManager $manager)
      {
          $files = [
              'category' =&gt; __DIR__.'/../../../AppBundle/DataFixtures/category.yml',
              'type'     =&gt; __DIR__.'/../../../AppBundle/DataFixtures/type.yml',
              //... other yml files
          ];
  
          $loader    = new NativeLoader();
          $objectSet = $loader-&gt;loadFiles($files);
  
          foreach ($objectSet-&gt;getObjects() as $key =&gt; $object) {
              $this-&gt;addReference($key, $object);
              $manager-&gt;persist($object);
          }
  
          $manager-&gt;flush();
      }
  }
  </code></pre>
  
  <p>And then I load my Article fixture with <code>DependentFixtureInterface::getDependencies</code> to be sure categories and types are already loaded</p>
  
  <pre><code>class ArticleFixtures extends Fixture implements DependentFixtureInterface
  {
      public function load(ObjectManager $manager)
      {
          // This give me my object
          dump($this-&gt;getReference('category_1'));
  
  
          $files = [
              'article' =&gt; __DIR__.'/../../../AppBundle/DataFixtures/article.yml',
              //... other yml files
          ];
  
          $loader    = new NativeLoader();
          $objectSet = $loader-&gt;loadFiles($files);
  
          foreach ($objectSet-&gt;getObjects() as $key =&gt; $object) {
              $manager-&gt;persist($object);
          }
  
          $manager-&gt;flush();
      }
  
      public function getDependencies()
      {
          return [
              BasicFixtures::class,
          ];
      }
  }
  </code></pre>
  
  <p>But it seams with that, the reference of "@category_1" is lost</p>
  
  <pre><code>In FixtureNotFoundExceptionFactory.php line 23:
  
    [Nelmio\Alice\Throwable\Exception\Generator\Resolver\FixtureNotFoundException]  
    Could not find the fixture "category_1".
  </code></pre>
  
  <p>What did I do wrong?
  Many thanks for help</p>