PDOException when setting field value in node presave I have a hook_node_presave implementation to set the value of a node field based on one of the user-entered values.

The code for it looks like this:

function er_pr_node_presave($node) {
  .....
  foreach($nameResult as $row) {
    $node-&gt;field_co_pi[LANGUAGE_NONE][$i]['value'] = intval($row-&gt;entity_id);
  }

  dpm($node);
}


With the dpm function I can see the values in the node, and the correct value is set to the field_co_pi field. However, the placeholder field doesn't get set when the insert query for that custom field is ran, resulting in a PDOException:
Sounds like you should be setting target_id instead:

foreach ($nameResult as $row) {
  $node-&gt;field_co_pi[LANGUAGE_NONE][$i]['target_id'] = intval($row-&gt;entity_id);
}


Note that not all fields have the same data column (such as 'value'). Reference fields have target_id which holds the referenced entity id.

Aside from that, just make sure that it has a value before setting, or you will get the same error again.

You redacted other parts of the code, so, I can't see if you set $i or not. Are you incrementing that per loop with $i++? I just assumed it is a multivalued field since you are doing a foreach instead of setting [0] directly.