<p>Turns out I was inadvertently "emptying" my user field on form post. Essentially I had named one of the form fields "fields[customUserField]" which upon submitting the form, was saving an empty value into the "Users" field. Then my custom event would trigger and fetch the "Users" field thereby retrieving an empty "array".</p>

<p>So nothing is wrong with the code below:</p>

<pre><code>// get entry
$entry = craft()-&gt;entries-&gt;getEntryById($entryId);

// get ALL users related to entry
$users = craft()-&gt;elements-&gt;getCriteria(ElementType::User);
$users-&gt;status = null;
$users-&gt;relatedTo = [$entry];
$userIds = $users-&gt;ids();
</code></pre>

CC BY-SA 3.0
2016-10-30T20:01:49.607Z
2016-10-31T03:00:18.800Z
2016-10-31T03:00:18.800Z
Jarrett Barnett
<p>As the title says, I can successfully return the "existing user IDs" from an entry field if the "entries.saveEntry" event is called after saving the entry from the CMS admin area; however, if I save an entry via a front-end form, the same code produces empty results.</p>

<p>I've used xDebug to verify that the objects are the same line by line; somehow the data isn't there for $users->ids() for front-end triggered events.</p>

<pre><code>// get entry
$entry = craft()-&gt;entries-&gt;getEntryById($entryId); // elementType =&gt; "Entry"

// get ALL users related to entry
$users = craft()-&gt;elements-&gt;getCriteria(ElementType::User); // elementType =&gt; Craft\UserElementType
$users-&gt;status = null;
$users-&gt;relatedTo = [$entry]; // elementType =&gt; "Entry"
$userIds = $users-&gt;ids(); // elementType =&gt; Craft\UserElementType
</code></pre>

<p>Example result when triggered from backend (good):</p>

<pre><code>array(
    0 =&gt; 251,
    1 =&gt; 252,
    3 =&gt; 257
)
</code></pre>

<p>Example result when triggered from front-end (help!):</p>

<pre><code>array()
</code></pre>

CC BY-SA 3.0
2016-10-30T02:53:14.087Z
Maybe users are being eager loaded in one case and not the other? https://craftcms.com/docs/templating/eager-loading-elements
Brad Bell
CC BY-SA 3.0
2016-10-30T04:37:39.400Z
Interesting, I'll explore this theory further.
Jarrett Barnett
CC BY-SA 3.0
2016-10-30T05:21:01.430Z
Turns out I was inadvertently "emptying" my user field on form post. 

Essentially I had named one of form fields "fields[customUserFieldName]" which was causing that input to empty the field. Then after that information was saved, it would trigger my entries.saveEntry event method and of course by then, there were no existing ids() to be obtained.
Jarrett Barnett
CC BY-SA 3.0
2016-10-30T17:31:05.103Z
Ahh... nice catch.  Can you add what worked for you as an official answer in case someone else runs into it?
Brad Bell
<plugin-development><users><plugins><relations>
Fetching Existing Users In Users Field Doesnt Work When entries.saveEntry Event Triggerred From Frontend
# Fetching Existing Users In Users Field Doesnt Work When entries.saveEntry Event Triggerred From Frontend

(Asked by: username_0 on 2016-10-30)

As the title says, I can successfully return the "existing user IDs" from an entry field if the "entries.saveEntry" event is called after saving the entry from the CMS admin area; however, if I save an entry via a front-end form, the same code produces empty results.
I've used xDebug to verify that the objects are the same line by line; somehow the data isn't there for $users->ids() for front-end triggered events.
// get entry
$entry = craft()->entries->getEntryById($entryId); // elementType => "Entry"

// get ALL users related to entry
$users = craft()->elements->getCriteria(ElementType::User); // elementType => Craft\UserElementType
$users->status = null;
$users->relatedTo = [$entry]; // elementType => "Entry"
$userIds = $users->ids(); // elementType => Craft\UserElementType

Example result when triggered from backend (good):
array(
    0 => 251,
    1 => 252,
    3 => 257
)

Example result when triggered from front-end (help!):
array()

**Tags:** plugin-development, users, plugins, relations
> **Comments:**
> **username_1**: Maybe users are being eager loaded in one case and not the other? https://craftcms.com/docs/templating/eager-loading-elements  
> **username_0**: Interesting, I'll explore this theory further.  
> **username_0**: Turns out I was inadvertently "emptying" my user field on form post.   Essentially I had named one of form fields "fields[customUserFieldName]" which was causing that input to empty the field. Then after that information was saved, it would trigger my entries.saveEntry event method and of course by then, there were no existing ids() to be obtained.  
> **username_1**: Ahh... nice catch.  Can you add what worked for you as an official answer in case someone else runs into it?  

---

## 1 answers

### Answer by username_0 on 2016-10-30. Score: 1 | Accepted answer

Turns out I was inadvertently "emptying" my user field on form post. Essentially I had named one of the form fields "fields[customUserField]" which upon submitting the form, was saving an empty value into the "Users" field. Then my custom event would trigger and fetch the "Users" field thereby retrieving an empty "array".
So nothing is wrong with the code below:
// get entry
$entry = craft()->entries->getEntryById($entryId);

// get ALL users related to entry
$users = craft()->elements->getCriteria(ElementType::User);
$users->status = null;
$users->relatedTo = [$entry];
$userIds = $users->ids();


---

