Answers.Body: <p>You can also use <a href="https://craftcms.com/classreference/models/ElementCriteriaModel#find-detail" rel="nofollow">find()</a> method or better <a href="https://craftcms.com/classreference/models/ElementCriteriaModel#first-detail" rel="nofollow">first()</a> method.</p>
  
  <p><code>first()</code> returns <code>EntryModel</code> or <code>null</code>.</p>
  
  <pre><code>if( $user-&gt;favouriteEntriesField-&gt;first(['id' =&gt; $searchingForThisEntryId]) ) {
      craft()-&gt;userSession-&gt;setError(Craft::t('This document is already favourited.'));
  }
  </code></pre>
  
  <p>Here we take advantage of <a href="https://craftcms.com/classreference/models/BaseModel#setAttributes-detail" rel="nofollow">baseModel's setAttributes() method</a> that is being used by first() method.</p>
  
  <pre><code>/**
   *   @var searchingForThisEntry EntryModel
   */
  
  if( $user-&gt;favouriteEntriesField-&gt;first($searchingForThisEntry) ) {
      craft()-&gt;userSession-&gt;setError(Craft::t('This document is already favourited.'));
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-27T15:25:42.820Z
Answers.LastActivityDate: 2016-10-27T15:31:00.767Z
Answers.LastEditDate: 2016-10-27T15:31:00.767Z
Answers.UserDisplayName: Krzysztof Boduch
Answers.Body: <p>You can avoid looping by getting an array of the IDs of the favourite entries, then check to see if the specific Entry ID is in the array.</p>
  
  <pre><code>$favouriteEntryIds = $user-&gt;favouriteEntriesField-&gt;ids();
  if( in_array($searchingForThisEntryId, $favouriteEntryIds) ) {
      ... the Entry has already been favourited ...
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-27T12:40:40.620Z
Answers.LastActivityDate: 2016-10-27T14:16:13.157Z
Answers.LastEditDate: 2016-10-27T14:16:13.157Z
Answers.UserDisplayName: Steve Rowling
Body: <p>Users in my project have a Favourite Entries field. In my plugin I want to check whether a specific Entry is present in that field for that user. I'm passing the Entry ID to look for. I am not interested in getting the Entry itself - I just want to check to see if it's already in the Favourite Entries field or not.</p>
  
  <p>I'd hoped that this would work:</p>
  
  <pre><code>if( $user-&gt;favouriteEntriesField-&gt;id($searchingForThisEntryId)-&gt;total() === 0 ) {
      ... doesn't contain the Entry yet ...
  }
  </code></pre>
  
  <p>But it doesn't work ( Object of class Craft\ElementCriteriaModel could not be converted to string )</p>
  
  <p>[edit]</p>
  
  <p>A working solution is to loop through everything:</p>
  
  <pre><code>foreach ($user-&gt;favouriteEntriesField as $favouriteEntry) {
      MyPlugin::log("ID of loop iterations Favourite Entry: $favouriteEntry-&gt;id", LogLevel::Info);
  
      if($favouriteEntry-&gt;id === $searchingForThisEntryId) {
          craft()-&gt;userSession-&gt;setError(Craft::t('This document is already favourited.'));
  
          $this-&gt;documentFavourited = "yes";
      }
  }
  </code></pre>
  
Tags: <plugin-development><relations>
Title: How, in a plugin, can I check that a User's "Favourite Entries" field contains a specific Entry?
ThreadText: # How, in a plugin, can I check that a User's "Favourite Entries" field contains a specific Entry?
  
  (Asked by: username_0 on 2016-10-27)
  
  Users in my project have a Favourite Entries field. In my plugin I want to check whether a specific Entry is present in that field for that user. I'm passing the Entry ID to look for. I am not interested in getting the Entry itself - I just want to check to see if it's already in the Favourite Entries field or not.
  
  I'd hoped that this would work:
  
  ```
  if( $user->favouriteEntriesField->id($searchingForThisEntryId)->total() === 0 ) {
      ... doesn't contain the Entry yet ...
  }
  
  ```
  
  But it doesn't work ( Object of class Craft\ElementCriteriaModel could not be converted to string )
  
  [edit]
  
  A working solution is to loop through everything:
  
  ```
  foreach ($user->favouriteEntriesField as $favouriteEntry) {
      MyPlugin::log("ID of loop iterations Favourite Entry: $favouriteEntry->id", LogLevel::Info);
  
      if($favouriteEntry->id === $searchingForThisEntryId) {
          craft()->userSession->setError(Craft::t('This document is already favourited.'));
  
          $this->documentFavourited = "yes";
      }
  }
  
  ```
  
  **Tags:** plugin-development, relations
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2016-10-27. Score: 1 | Accepted answer
  
  You can also use [find()](https://craftcms.com/classreference/models/ElementCriteriaModel#find-detail) method or better [first()](https://craftcms.com/classreference/models/ElementCriteriaModel#first-detail) method.
  
  `first()` returns `EntryModel` or `null`.
  
  ```
  if( $user->favouriteEntriesField->first(['id' => $searchingForThisEntryId]) ) {
      craft()->userSession->setError(Craft::t('This document is already favourited.'));
  }
  
  ```
  
  Here we take advantage of [baseModel's setAttributes() method](https://craftcms.com/classreference/models/BaseModel#setAttributes-detail) that is being used by first() method.
  
  ```
  /**
   *   @var searchingForThisEntry EntryModel
   */
  
  if( $user->favouriteEntriesField->first($searchingForThisEntry) ) {
      craft()->userSession->setError(Craft::t('This document is already favourited.'));
  }
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2016-10-27. Score: 3
  
  You can avoid looping by getting an array of the IDs of the favourite entries, then check to see if the specific Entry ID is in the array.
  
  ```
  $favouriteEntryIds = $user->favouriteEntriesField->ids();
  if( in_array($searchingForThisEntryId, $favouriteEntryIds) ) {
      ... the Entry has already been favourited ...
  }
  
  ```
  
  
  ---
  
  