<p>Ah got it : </p>

<pre><code>$lNewUser-&gt;getContent()-&gt;setAttributes(array(
    'companiesDropdown' =&gt; array($aUser-&gt;companyId),
));
</code></pre>

CC BY-SA 3.0
2014-07-26T16:41:46.490Z
2014-07-26T16:41:46.490Z
pythic
<p>I'm building a plugin to add users to the system with set properties (like company) and permissions. The user form works with an "Entries" field and works with connected source section entries and works fine. Now when I receive the objects in my SiteAdministrator_UserService i'm trying to link the passed in companyId to the custom Entries field. Like this:</p>

<pre><code>$lNewUser = new UserModel();
$lNewUser-&gt;status = UserStatus::Pending;
$lNewUser-&gt;firstName = $aUser-&gt;firstname;
$lNewUser-&gt;username = $aUser-&gt;username;
$lNewUser-&gt;lastName = $aUser-&gt;lastname;
$lNewUser-&gt;email = $aUser-&gt;emailaddress;

$lCompany = craft()-&gt;entries-&gt;getEntryById($aUser-&gt;companyId);
$lNewUser-&gt;getContent()-&gt;setAttributes(array(
        'companiesDropdown' =&gt; $lCompany,
    ));

return craft()-&gt;users-&gt;saveUser($lNewUser);
</code></pre>

<p>$lCompany holds the entry of the company, that works fine. Now how do I link the $lCompany to the first entry in the custom Entries (companiesDropdown handle) field?</p>

<p>Just to clarify. All the above has nothing to do with an actual dropdown field.
Thanks in advance</p>

<plugin-development><users>
How to add/link an entry to an object using PHP?
# How to add/link an entry to an object using PHP?

(Asked by: username_0 on 2014-07-26)

I'm building a plugin to add users to the system with set properties (like company) and permissions. The user form works with an "Entries" field and works with connected source section entries and works fine. Now when I receive the objects in my SiteAdministrator_UserService i'm trying to link the passed in companyId to the custom Entries field. Like this:
$lNewUser = new UserModel();
$lNewUser->status = UserStatus::Pending;
$lNewUser->firstName = $aUser->firstname;
$lNewUser->username = $aUser->username;
$lNewUser->lastName = $aUser->lastname;
$lNewUser->email = $aUser->emailaddress;

$lCompany = craft()->entries->getEntryById($aUser->companyId);
$lNewUser->getContent()->setAttributes(array(
        'companiesDropdown' => $lCompany,
    ));

return craft()->users->saveUser($lNewUser);

$lCompany holds the entry of the company, that works fine. Now how do I link the $lCompany to the first entry in the custom Entries (companiesDropdown handle) field?
Just to clarify. All the above has nothing to do with an actual dropdown field.
Thanks in advance

**Tags:** plugin-development, users

---

## 1 answers

### Answer by username_0 on 2014-07-26. Score: 4

Ah got it : 
$lNewUser->getContent()->setAttributes(array(
    'companiesDropdown' => array($aUser->companyId),
));


---

