Insert entry into article field (elementselect?) on a user, with the "onBeforeSaveEntry" hook 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()-&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);

    }
});


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
Sweet, solved it.

Changed the line

array_push($focusList, $entry);


to

array_push($focusList, $entry-&gt;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()-&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);
    }
});