title: doctrine2 extra lazy fetching of association
body: <p>I have a <code>Thread</code> entity which has a <code>OneToMany</code> association with a <code>Message</code> entity. I am fetching a thread with a DQL query, and I want to limit its amount of messages to 10. Therefore I am setting the fetch mode to <code>EXTRA_LAZY</code> as below.</p>
  
  <pre><code>class Thread
  {
      // ...
  
      /**
       * @var ArrayCollection
       * @ORM\OneToMany(targetEntity="Profile\Entity\Message", mappedBy="thread", fetch="EXTRA_LAZY")
       * @ORM\OrderBy({"timeSent" = "ASC"})
       */
      protected $messages;
  }
  </code></pre>
  
  <p>This allows me to use the <code>slice</code> method to issue a <code>LIMIT</code> SQL query to the database. All good so far. Because my messages are encrypted, I need to decrypt them in my service layer before handling the thread object off to the controller (and ultimately view). To accomplish this, I am doing the following in my service:</p>
  
  <pre><code>foreach ($thread-&gt;getMessages()-&gt;slice(0, 10) as $message) {
      // Decrypt message
  }
  </code></pre>
  
  <p>The call to <code>slice</code> triggers an SQL query that fetches 10 messages. In my view, I am doing the following to render the thread's messages:</p>
  
  <pre><code>$this-&gt;partialLoop()-&gt;setObjectKey('message');
  echo $this-&gt;partialLoop('partial/thread/message.phtml', $thread-&gt;getMessages());
  </code></pre>
  
  <p>The problem is that this fetches the entire collection of messages from the database. If I call <code>slice</code> as in my service, the same SQL query with <code>LIMIT 10</code> is issued to the database, which is not desirable.</p>
  
  <p>How can I process a limited collection of messages in my service layer without issuing another SQL query in my view? That is, to have doctrine create a single SQL query, not two. I could simply decrypt my messages in my view, but that kind of defeats the purpose of having a service layer in this case. I could surely fetch the messages "manually" and add them to the thread object, but if I could do it automatically through the association, then that would be much preferred.</p>
  
  <p>Thanks in advance!</p>