title: Symfony Doctrine Autoload from relation Many-to-One/One-to-Many
body: <p>I have 2 Main entities and one relation table in my Symfony/Doctrine App.
  My goal is to load them Easily.</p>
  <p>I have Gallery class that looks like this:</p>
  <pre class="lang-php prettyprint-override"><code>class Gallery
  {
      /**
       * @var int
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type=&quot;integer&quot;, options={&quot;unsigned&quot;=true})
       */
      private $id;
  
      /**
       * @var string
       * @ORM\Column(type=&quot;string&quot;, length=100, nullable=false)
       */
      private $title;
  
      /**
       * @var Collection
       * @ORM\OneToMany(targetEntity=GalleryHasImage::class, mappedBy=&quot;gallery&quot;)
       */
      private $hasImages;**strong text**
  </code></pre>
  <p>Image class:</p>
  <pre class="lang-php prettyprint-override"><code>class Image implements ImageInterface
  {
      /**
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type=&quot;integer&quot;, options={&quot;unsigned&quot;=true})
       */
      private $id;
  
      /**
       * @ORM\Column(type=&quot;string&quot;, length=255, nullable=false)
       */
      private $title;
  </code></pre>
  <p>and Relation table with extra data:</p>
  <pre class="lang-php prettyprint-override"><code>class GalleryHasImage
  {
      /**
       * @ORM\Id()
       * @ORM\GeneratedValue()
       * @ORM\Column(type=&quot;integer&quot;, options={&quot;unsigned&quot;=true})
       */
      private $id;
  
      /**
       * @var Gallery
       * @ORM\ManyToOne(targetEntity=&quot;App\Entity\Gallery\Gallery&quot;, inversedBy=&quot;hasImages&quot;)
       */
      private $gallery;
  
      /**
       * @var Image
       * @ORM\ManyToOne(targetEntity=&quot;App\Entity\Image\Image&quot;, inversedBy=&quot;hasGalleries&quot;)
       */
      private $image;
  
      /**
       * @ORM\Column(type=&quot;timestamp&quot;, options={&quot;unsigned&quot;=true})
       */
      private DateTimeInterface $createdAt;
  
      /**
       * @ORM\ManyToOne(targetEntity=User::class)
       * @ORM\JoinColumn(nullable=false)
       */
      private $createdBy;
  
      /**
       * @ORM\Column(type=&quot;integer&quot;, options={&quot;unsigned&quot;=true})
       */
      private int $position;
  </code></pre>
  <p>My goal is to have simple getter in my Gallery Class to get images without position. Something like</p>
  <pre class="lang-php prettyprint-override"><code>/**
       * @Route(&quot;/gallery/{id}/images&quot;, methods={&quot;GET&quot;}, requirements={&quot;id&quot;:&quot;\d+&quot;})
       * @param Gallery $gallery)
       * @return Response
       */
  public function getImages(Gallery $gallery)
  {
     print_r($gallery-&gt;getImages());
  }
  </code></pre>
  <p>Right now I am only able to do it with extra steps like:</p>
  <pre class="lang-php prettyprint-override"><code>/**
   * @Route(&quot;/gallery/{id}/images&quot;, methods={&quot;GET&quot;}, requirements={&quot;id&quot;:&quot;\d+&quot;})
   * @param Gallery $gallery)
   * @return Response
   */
  public function getImages(Gallery $gallery)
  {
      foreach($gallery-&gt;getHasImages as $hasImage)
      { 
          print_r($hasImage-&gt;getImage());
      }
  }
  </code></pre>