Hi folks.
I have a module that's creating new nodes, and I need to insert some references to users.

My field is called field_email_previous_recipients' .
It's an Entity reference with target type = 'user', unlimited values, and Simple Entity Selection mode.

The rest of my node creation code is working fine, but i can't add user references in this field.

My entity reference insertion code looks like this:

 $node->field_email_previous_recipients[$node->language][] = array(
        'field_email_previous_recipients_target_id' => $theRecipientUID,
      );

When I use this, I get

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'field_email_previous_recipients_target_id' cannot be null:

I've tried hard-coding a number into the array; and I can verify that the $theRecipientUID is not in fact null, but nothing's getting me past the SQL complaint.

Any ideas what to do next would be greatly appreciated.

Comments

Sam Moore’s picture

::DOH:: - I should have been using

        'target_id' => $theRecipientUID

rather than

        'field_email_previous_recipients_target_id' => $theRecipientUID,

I got field_email_previous_recipients_target_id from looking at the field names in phpMyAdmin - that led me astray.
I'll leave this here in case it helps anyone else.

nitin.k’s picture

Same case happens when you attach some file to the node, at that time file information is supplied to the node field..



                file_usage_add($file_info, 'file', 'node', $node->nid);
                $node->field_image[LANGUAGE_NONE][] = array(
                    'fid' => $file_info->fid,
                    'filename' => $file_info->filename,
                    'filemime' => $file_info->filemime,
                    'uid' => $user->uid,
                    'uri' => $file_info->uri,
                    'status' => 1
                );

Sam Moore’s picture

You'll want to make a new post for this, as mine is marked "Solved".