1

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->field_co_pi[LANGUAGE_NONE][$i]['value'] = intval($row->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:

enter image description here

1

Sounds like you should be setting target_id instead:

foreach ($nameResult as $row) {
  $node->field_co_pi[LANGUAGE_NONE][$i]['target_id'] = intval($row->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.

  • Yes, its in a for loop with $i being incremented. – tylero Mar 16 '17 at 20:39
  • Okay, well, change your item to target_id instead of value then, as thats all I can see is wrong here. – Kevin Mar 16 '17 at 20:40
  • Yep that fixed it. – tylero Mar 16 '17 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.