title: sort objects with several parameters - symfony2
body: <p>I want to display a list of objects ordered by a parameter that depends of several fields of the entity.</p>
  
  <p>I have created an entity, let's call it Object for instance, which has an id and 2 integers as fields. I work with Symfony 2 and Doctrine as ORM.</p>
  
  <pre><code>  `/**
   * Object
   *
   * @ORM\Table(name="object")
   * @ORM\Entity(repositoryClass="AppBundle\Repository\ObjectRepository")
   */
   class Object
   {
       /**
        * @var int
        *
        * @ORM\Column(name="id", type="integer")
        * @ORM\Id
        * @ORM\GeneratedValue(strategy="AUTO")
        */
       private $id;
  
       /**
        * @var int
        *
        * @ORM\Column(name="field1", type="int")
        */
       private $field1;
  
       /**
        * @var int
        *
        * @ORM\Column(name="field2", type="int")
        */
       private $field2;
   }
  </code></pre>
  
  <p>Now I would like to get the top-5 instances of Object from my database ordered by, for example, the average between field1 and field2, to display them in a twig template.</p>
  
  <p>I know I can use this:</p>
  
  <pre><code>$list = $this-&gt;getDoctrine()-&gt;getRepository('AppBundle:Object')-&gt;findAll();
  </code></pre>
  
  <p>to get a list of all existing instances of Object in the DB, but although it might be a relatively simple question to experimented Symfony programmers, I have no idea about how to order it by avg(field1,field2).</p>