title: How to use Doctrine's ArrayCollection::exists method
body: <p>I have to check if an <code>Email</code> entity already exists in the <code>ArrayCollection</code> but I have to perform the check on the emails as strings (the Entity contains an ID and some relations to other entites, for this reason I use a separate table that persists all emails).</p>
  
  <p>Now, in the first I wrote this code:</p>
  
  <pre><code>    /**
       * A new Email is adding: check if it already exists.
       *
       * In a normal scenario we should use $this-&gt;emails-&gt;contains().
       * But it is possible the email comes from the setPrimaryEmail method.
       * In this case, the object is created from scratch and so it is possible it contains a string email that is
       * already present but that is not recognizable as the Email object that contains it is created from scratch.
       *
       * So we hav to compare Email by Email the string value to check if it already exists: if it exists, then we use
       * the already present Email object, instead we can persist the new one securely.
       *
       * @var Email $existentEmail
       */
      foreach ($this-&gt;emails as $existentEmail) {
          if ($existentEmail-&gt;getEmail()-&gt;getEmail() === $email-&gt;getEmail()) {
              // If the two email compared as strings are equals, set the passed email as the already existent one.
              $email = $existentEmail;
          }
      }
  </code></pre>
  
  <p>But reading the <code>ArrayCollection</code> class I seen the method <a href="https://github.com/doctrine/collections/blob/master/lib/Doctrine/Common/Collections/ArrayCollection.php#L233-L239" rel="noreferrer"><code>exists</code></a> that seems to be a more elgant way of doing the same thing I did.</p>
  
  <p>But I don't know how to use it: can someone explain me how to use this method given the code above?</p>