title: Get similar entities in Symfony using Doctrine
body: <p>I have a Entity in Symfony for Videos. Lets say the db looks like this:</p>
  
  <pre><code>id: int
  name: varchar
  uploader: int (User Entity)
  video_mp4: varchar
  param1: int
  param2: int
  param3: int
  </code></pre>
  
  <p>I'd like to load similar videos. Similar means:</p>
  
  <ul>
  <li>Or same Uploader</li>
  <li>Or same Param1</li>
  <li>Or same Param2</li>
  <li>Or same Param3</li>
  </ul>
  
  <p>Actually I'd like to order them by the most matchings (meaning a video uploaded by the same user and with same param1 + param3 should be more important then one where just param2 is matching) something like <code>ORDER BY mostMatches DESC</code> any ideas how to achive that
  For this I'd use this query (yes I know that "OR" isn't good for the DB, sugestions welcome ;))</p>
  
  <p>I created in my Entity class a function that looks like this: ($q is a QueryBuilder)</p>
  
  <pre><code>public function getRelated($q, $limit = 6) {
      $uploader = $this-&gt;getUploader()-&gt;getId();
      $param1 = $this-&gt;getParam1();
      $param2 = $this-&gt;getParam2();
      $param3 = $this-&gt;getParam3();
  
      $q-&gt;select('e')
      -&gt;from('MyBackendBundle:Video','e')
      -&gt;where('e.id != ?0')
      -&gt;andWhere('e.uploader = ?1 OR e.param1 = ?2 OR e.param2 = ?3 OR e.param3 = ?4');
  
      if ($limit &gt; 0) {
          $q-&gt;setMaxResults($limit);
      }
  
      $q-&gt;setParameters(array(
          $this-&gt;id,
          $uploader,
          $param1,
          $param2,
          $param3
      ));
  
      return $q-&gt;getQuery()-&gt;getResult();
  }
  </code></pre>
  
  <p>Two questions:</p>
  
  <ol>
  <li>How can I get the QueryBuilder in an Entity, I don't like to pass it from my Controller.</li>
  <li>Is there a better way to load similar entries?</li>
  <li>How can I order them by the most similarities (uploader, param1, param2, param3), sure I could do this with PHP but what if result 7 would be a 100% match but I load only 6?</li>
  </ol>