title: PHP Pattern - how to retrieve collection of objects
body: <p><strong>Situation:</strong></p>
  
  <p>There is simple <code>User</code> class which is Doctrine entity (I skipped comments to keep the code short):</p>
  
  <pre><code>class User {
    protected $iUserId;
    protected $sName;
  }
  </code></pre>
  
  <p><strong>Question:</strong><br>
  How to retrieve collection of objects of class <code>User</code> from the controller? </p>
  
  <p><strong>Follow up:</strong>
  Until now, we were creating methods like <code>getUsers()</code> in <code>User</code> class which gets data from DB, creates <code>User</code> class objects and returns it.</p>
  
  <p>Now, I'm wondering if it's not a better solution to create class like <code>UserCollection</code> which would handle data retrieving and creating <code>User</code> objects? Maybe I should make use of <code>\Doctrine\Common\Collections\Collection</code> class somehow? </p>
  
  <p>What I'd like to accomplish is easy way to handles i.e. <code>where</code> clauses. <code>UserCollection</code> class could have <code>QueryBuilder</code> object on which I could operate from controller like this:</p>
  
  <pre><code>$oUserCollection = new UserCollection();
  $oUserCollection-&gt;setWhere( 'u.iUserId = 1' );
  $aUsers = oUserCollection-&gt;getUsers();
  ...
  </code></pre>
  
  <p>Please share your thoughts on that topic.</p>
  
  <p><strong>Update:</strong>
  Doctrine provides a <a href="http://docs.doctrine-project.org/en/latest/reference/working-with-objects.html#custom-repositories" rel="nofollow">concept of repositories</a> of entities which I think maybe the solution I'm looking for.</p>