How, in a plugin, can I check that a User's "Favourite Entries" field contains a specific Entry? 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-&gt;favouriteEntriesField-&gt;id($searchingForThisEntryId)-&gt;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-&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";
    }
}
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-&gt;favouriteEntriesField-&gt;ids();
if( in_array($searchingForThisEntryId, $favouriteEntryIds) ) {
    ... the Entry has already been favourited ...
}