Answers.Body: <p>Sweet, solved it.</p>
  
  <p>Changed the line</p>
  
  <pre><code>array_push($focusList, $entry);
  </code></pre>
  
  <p>to</p>
  
  <pre><code>array_push($focusList, $entry-&gt;id);
  </code></pre>
  
  <p>and it worked! :D</p>
  
  <p><br><br>
  EDIT: NOPE, it did not fully work. This hook overwrites all the other entries, leaving only the most recent inside the list.</p>
  
  <p>This code does work, without overwriting: </p>
  
  <pre><code>parent::init();
  craft()-&gt;on('entries.onSaveEntry', function(Event $event) {
      $entry = $event-&gt;params['entry'];
      $isNew = $event-&gt;params['isNewEntry'];
  
      if ($isNew) {
          $user = craft()-&gt;users-&gt;getUserById($entry['authorId']);
          $focusList = $user-&gt;getFieldValue('focusList');
          $values = [];
  
  
          foreach ($focusList as $focusEntry) {
              array_push($values, $focusEntry-&gt;id);
          }
  
          array_push($values, $entry-&gt;id);
  
  
          $user-&gt;setContentFromPost(array(
              'focusList' =&gt; $values
           ));
          craft()-&gt;users-&gt;saveUser($user);
      }
  });
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-07T12:32:15.120Z
Answers.LastActivityDate: 2016-06-07T20:40:27.330Z
Answers.LastEditDate: 2016-06-07T20:40:27.330Z
Answers.UserDisplayName: Ole
Body: <p>I'm having a hard time inserting new content into an elementselect field for a user. </p>
  
  <p>Here's the code I'm working on so far, inside a custom plugin init()</p>
  
  <pre><code>parent::init();
  craft()-&gt;on('entries.onBeforeSaveEntry', function(Event $event) {
      $entry = $event-&gt;params['entry'];
      $isNew = $event-&gt;params['isNewEntry'];
  
      if ($isNew) {
          $user = craft()-&gt;users-&gt;getUserById($entry['authorId']);
  
          $focusList = $user-&gt;getContent()-&gt;focusList;
  
          if (!is_array($focusList)) {
              $focusList = array();
          }
          array_push($focusList, $entry);
          $user-&gt;getContent()-&gt;focusList = $focusList;
  
          craft()-&gt;users-&gt;saveUser($user);
  
      }
  });
  </code></pre>
  
  <p>I'm tring to add a "onBeforeSaveEntry" hook, that first checks if the entry is new. </p>
  
  <p>If it is, add the entry to the authors "focusList" (an article field without limits) </p>
  
  <p>But I can't get it to work. Any suggestions?</p>
  
  <p><br></p>
  
  <p>The error message I'm stuck with so far:</p>
  
  <pre><code>"CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails" is the error message I'm battling
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-06-07T12:27:02.843Z
Comments.Text: Tried to change the hook to "entries.onSaveEntry", and the error message turns into "var_export does not handle circular references"
Comments.UserDisplayName: Ole
Tags: <plugin-development><entries><users><array><hook>
Title: Insert entry into article field (elementselect?) on a user, with the "onBeforeSaveEntry" hook
ThreadText: # Insert entry into article field (elementselect?) on a user, with the "onBeforeSaveEntry" hook
  
  (Asked by: username_0 on 2016-06-07)
  
  I'm having a hard time inserting new content into an elementselect field for a user.
  
  Here's the code I'm working on so far, inside a custom plugin init()
  
  ```
  parent::init();
  craft()->on('entries.onBeforeSaveEntry', function(Event $event) {
      $entry = $event->params['entry'];
      $isNew = $event->params['isNewEntry'];
  
      if ($isNew) {
          $user = craft()->users->getUserById($entry['authorId']);
  
          $focusList = $user->getContent()->focusList;
  
          if (!is_array($focusList)) {
              $focusList = array();
          }
          array_push($focusList, $entry);
          $user->getContent()->focusList = $focusList;
  
          craft()->users->saveUser($user);
  
      }
  });
  
  ```
  
  I'm tring to add a "onBeforeSaveEntry" hook, that first checks if the entry is new.
  
  If it is, add the entry to the authors "focusList" (an article field without limits)
  
  But I can't get it to work. Any suggestions?
  
  The error message I'm stuck with so far:
  
  ```
  "CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails" is the error message I'm battling
  
  ```
  
  **Tags:** plugin-development, entries, users, array, hook
  
  > **Comments:**
  > **username_0**: Tried to change the hook to "entries.onSaveEntry", and the error message turns into "var_export does not handle circular references"  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2016-06-07. Score: 2
  
  Sweet, solved it.
  
  Changed the line
  
  ```
  array_push($focusList, $entry);
  
  ```
  
  to
  
  ```
  array_push($focusList, $entry->id);
  
  ```
  
  and it worked! :D
  
  EDIT: NOPE, it did not fully work. This hook overwrites all the other entries, leaving only the most recent inside the list.
  
  This code does work, without overwriting:
  
  ```
  parent::init();
  craft()->on('entries.onSaveEntry', function(Event $event) {
      $entry = $event->params['entry'];
      $isNew = $event->params['isNewEntry'];
  
      if ($isNew) {
          $user = craft()->users->getUserById($entry['authorId']);
          $focusList = $user->getFieldValue('focusList');
          $values = [];
  
          foreach ($focusList as $focusEntry) {
              array_push($values, $focusEntry->id);
          }
  
          array_push($values, $entry->id);
  
          $user->setContentFromPost(array(
              'focusList' => $values
           ));
          craft()->users->saveUser($user);
      }
  });
  
  ```
  
  
  ---
  
  