title: How to get object from database in Fixtures Symfony
body: <p>I want to create fake posts with PostFixtures class . I use fake data with "title, slug, content, pushlish_at". But with "author" , i want use data from database (user table). I don't know how to get it. Please help me!</p>
  
  <p>Here is my code : </p>
  
  <pre><code>&lt;?php
  
  namespace App\DataFixtures;
  
  use Faker\Factory;
  use App\Entity\Post;
  use App\Entity\User;
  use Doctrine\Bundle\FixturesBundle\Fixture;
  use Doctrine\Persistence\ObjectManager;
  
  
  class PostFixtures extends Fixture
  {
      public function load(ObjectManager $manager)
      {
          $faker = Factory::create();
  
          for ($i = 0; $i&lt; 10; $i++) {
              $post = new Post();
  
              $post-&gt;setTitle($faker-&gt;title());
              $post-&gt;setSlug($faker-&gt;slug());
              $post-&gt;setContent($faker-&gt;paragraph());
              $post-&gt;setPublishAt($faker-&gt;dateTimeBetween('-100 days', '-1 days'));
  
              $userRepository = $manager-&gt;getRepository(User::class);
              $user = $userRepository-&gt;find(rand(1, 3));
              $post-&gt;setAuthor($user);
  
              $manager-&gt;persist($post);
          }
  
          $manager-&gt;flush();
      }
  
  }
  
  </code></pre>
  
  <p>In database, I just have three users (id from 1 to 3). And I created ManyToOne relation between Post and User class. But I don't know why $user is null.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p><code>UserFixtures</code> class:</p>
  
  <pre><code>&lt;?php
  
  namespace App\DataFixtures;
  
  use App\Entity\User;
  use Doctrine\Bundle\FixturesBundle\Fixture;
  use Doctrine\Persistence\ObjectManager;
  use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  
  class UserFixtures extends Fixture
  {
      public const USER_REFERENCE = 'users';
      private $passwordEncoder;
  
      /**
       * UserFixtures constructor.
       */
      public function __construct(UserPasswordEncoderInterface $passwordEncoder)
      {
          $this-&gt;passwordEncoder = $passwordEncoder;
      }
  
  
      public function load(ObjectManager $manager)
      {
          $user1 = new User();
          $user1-&gt;setEmail('tien@gmail.com');
          $user1-&gt;setPassword(
              $this-&gt;passwordEncoder-&gt;encodePassword(
                  $user1,
                  '123456'
              )
          );
          $user1-&gt;setRoles(['ROLE_ADMIN']);
          $manager-&gt;persist($user1);
          $manager-&gt;flush();
          dump($user1); // it still has value
          $this-&gt;addReference(self::USER_REFERENCE, $user1);
      }
  }
  
  </code></pre>
  
  <p><code>PostFixtures</code> class: </p>
  
  <pre><code>&lt;?php
  
  namespace App\DataFixtures;
  
  use Faker\Factory;
  use App\Entity\Post;
  use App\Entity\User;
  use Doctrine\Bundle\FixturesBundle\Fixture;
  use Doctrine\Persistence\ObjectManager;
  use Doctrine\Common\DataFixtures\DependentFixtureInterface;
  
  class PostFixtures extends Fixture implements DependentFixtureInterface
  {
      public function load(ObjectManager $manager)
      {
          $faker = Factory::create('en_US');
  
          $user =  $this-&gt;getReference(UserFixtures::USER_REFERENCE);
          dd($user);  // I want to check it: $user still has id . But 
                     // email,role and password are null
  
          for ($i = 0; $i &lt; 10; $i++) {
              $post = new Post();
              $post-&gt;setTitle($faker-&gt;title());
              $post-&gt;setSlug($faker-&gt;slug());
              $post-&gt;setContent($faker-&gt;paragraph());
              $post-&gt;setPublishAt($faker-&gt;dateTimeBetween('-100 days', '-1 days'));
              $post-&gt;setAuthor($user);
              $manager-&gt;persist($post);
          }
          $manager-&gt;flush();
      }
  
      public function getDependencies()
      {
          return array(
              UserFixtures::class,
          );
      }
  
  }
  
  </code></pre>