title: Doctrine2: check if exists value in Doctrine Collection
body: <p>How can I check that given value exists in <strong>Doctrine Collection</strong> (ManyToMany relation) field?</p>
  
  <p>For example I try to:</p>
  
  <pre><code>$someClass = $this-&gt;
               getDoctrine()-&gt;
               getRepository('MyBundle:MyClass')-&gt;
               find($id);
  
  if (!$entity-&gt;getMyCollectionValues()-&gt;get($someClass-&gt;getId())) {
  
      $entity-&gt;addMyCollectionValue($someClass);
  
  }
  </code></pre>
  
  <p>But it is of course not correct. So, how to avoid for duplicate keys?</p>