title: Many to many relation and query builder
body: <p>I would like to extend my user repository. I will find all users with a specified role or
  that satisfy an additional condition.</p>
  
  <p>I try some queries with the query builder, but without success.</p>
  
  <p>Here is an example of my database structure.</p>
  
  <p>Tablename: <strong>User</strong> </p>
  
  <pre><code>+----+----------+-----------+
  | id | isActive | isDeleted |
  +----+----------+-----------+
  |  1 |        1 |         0 |
  +----+----------+-----------+
  </code></pre>
  
  <p>Tablename: <strong>Role</strong></p>
  
  <pre><code>+----+-------+
  | id | name  |
  +----+-------+
  |  1 | Admin |
  +----+-------+
  </code></pre>
  
  <p>Tablename: <strong>user_role</strong></p>
  
  <p>ManyToMany relation between user and role</p>
  
  <pre><code>+---------+---------+
  | user_id | role_id |
  +---------+---------+
  |       1 |       1 |
  +---------+---------+
  </code></pre>
  
  <p>Tablename: <strong>distance</strong></p>
  
  <p>OneToMany relation between user and table distance</p>
  
  <pre><code>+----+---------+-----+------+
  | id | user_id | lat | long |
  +----+---------+-----+------+
  |  1 |       1 |     |      |
  +----+---------+-----+------+
  </code></pre>
  
  <p>I write a plain sql query, which is correct for me, but i do not know how can i realize
  the many to many relation with the query builder.</p>
  
  <pre><code>SELECT u.id 
  FROM user as u,  user_role  as ur 
  WHERE u.id=ur.user_id and ur.role_id=1 OR 
  (u.id not in (select user_id from distance) and ur.role_id=1 and u.id=ur.user_id);
  </code></pre>
  
  <p>Can someone give me a hint how I can solve this issue?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I will show only some parts of the entities, because of readability and clarity.</p>
  
  <pre><code>class User implements AdvancedUserInterface, \Serializable
  {
      /**
       * @ORM\Column(type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       * @Exclude
       */
      protected $id;
  
      /**
       * @ORM\ManyToMany(targetEntity="App\UserBundle\Entity\Role", inversedBy="users")
       *
       */
      protected $roles;
  
      /**
       * @ORM\OneToMany(targetEntity="App\UserBundle\Entity\Distance", mappedBy="user", cascade={"persist", "remove"})
       * @Exclude
       */
      protected $distance;
  
  }
  
  
  class Role implements RoleInterface
  {
  
      /**
       * @ORM\Column(name="id", type="integer")
       * @ORM\Id()
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @ORM\Column(name="name", type="string", length=255)
       * @Exclude
       */
      private $name;
  
      /**
       * @ORM\ManyToMany(targetEntity="App\UserBundle\Entity\User", mappedBy="roles")
       */
      private $users;
  }
  
  
  class Distance
  {
      /**
       * @ORM\Column(type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;
  
      /**
       * @ORM\ManyToOne(targetEntity="User", inversedBy="distance", cascade={"persist", "remove"})
       * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
       */
      protected $user;
  
  }
  </code></pre>