How can I programmatically set the value of an Entries field type?
For relationship fields (Entries, Assets, Users, Tags, etc.) you need to pass in an array of element IDs.

Something like this should work.

$entry-&gt;setAttribute('companyField', array($companyEntry-&gt;id));