title: Eager loading of related entity in Symfony 2
body: <p>There are three entities: Customer, Messages, Attachments.</p>
  
  <p>The relationship between these entities is straight forward: A customer can have many messages and a message can have many attachments. Both relations are "one-to-many".</p>
  
  <p>I told doctrine to be lazy when loading the messages for the Customer entity. So <code>$customer-&gt;getMessages()</code> results in an additional SQL statement. That's fine.</p>
  
  <p>But I also defined an "EAGER" loading for the attachments for the Message entity.</p>
  
  <p>Now I would have expected that the messages I get by calling <code>$customer-&gt;getMessages()</code> are already loaded with all their attachments. But <code>$message-&gt;getAttachments()</code> still causes one SQL statement per message.</p>
  
  <p>Is this behavior expected?</p>
  
  <p>Just for reference, excepts from my classes:</p>
  
  <p>Customer.php</p>
  
  <pre><code>class Customer
  {
      /**
       * @ORM\OneToMany(targetEntity="Message", mappedBy="customer")
       * @ORM\OrderBy({"createdOn" = "DESC"})
       */
      private $messages;
  </code></pre>
  
  <p>Message.php</p>
  
  <pre><code>class Message
  {
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @ORM\ManyToOne(targetEntity="Customer", inversedBy="messages")
       * @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
       **/
      private $customer;
      /**
       * @ORM\OneToMany(targetEntity="Attachment", mappedBy="message", fetch="EAGER")
       **/
      private $attachments;
  </code></pre>
  
  <p>Attachment.php:</p>
  
  <pre><code>class Attachment
  {
  
      /**
       * @var integer
       *
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @ORM\ManyToOne(targetEntity="Message", inversedBy="attachments")
       * @ORM\JoinColumn(name="message_id", referencedColumnName="id")
       **/
      private $message;
  </code></pre>