1

Problem

How do I set a user reference value from the reference module using an entity metadata wrapper?

So Far

I have a form submit. It's creating an node with a user reference field. I can't work out what structure will submit the value.

First I tried submitting a user account (object), however this throws an awful error.

global $user;
$ewrapper->field_user_permission->set($user);

I then tried submitting uid, however it said it was expecting an array not a string.

$ewrapper->field_user_permission->set($user->uid);

I then tried creating a custom array based on printing out the array of a field that was already set.

enter image description here

$user_perm = array (
  'uid'    => $account->uid,
  'access' => TRUE,
);

 $ewrapper->field_user_permission->set($user_perm);

But that printed out another horrible error. What should I be using to submit the user reference?

 PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'uid' for column 'delta' at row 1: INSERT INTO {field_data_field_user_permission} (entity_type, entity_id, revision_id, bundle, delta, language, field_user_permission_uid) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6), (:db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13); Array ( [:db_insert_placeholder_0] => node [:db_insert_placeholder_1] => 246 [:db_insert_placeholder_2] => 246      [:db_insert_placeholder_3] => contact_details [:db_insert_placeholder_4] => uid [:db_insert_placeholder_5] => und [:db_insert_placeholder_6] => 182 [:db_insert_placeholder_7] => node [:db_insert_placeholder_8] => 246 [:db_insert_placeholder_9] => 246 [:db_insert_placeholder_10] => contact_details [:db_insert_placeholder_11] => access [:db_insert_placeholder_12] => und [:db_insert_placeholder_13] => 1 ) in field_sql_storage_field_storage_write() (line 451 of C:\XAMPP\htdocs\drupal\modules\field\modules\field_sql_storage\field_sql_storage.module).

EDIT

New error for creating a new level:

$user_perm[] = array (
  'uid'    => $account->uid,
  'access' => TRUE,
);

$ewrapper->field_user_permission->set($user_perm);

PDOException: SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1: INSERT INTO {field_data_field_user_permission} (entity_type, entity_id, revision_id, bundle, delta, language, field_user_permission_uid) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6_uid, :db_insert_placeholder_6_access); Array ( [:db_insert_placeholder_0] => node [:db_insert_placeholder_1] => 253 [:db_insert_placeholder_2] => 253 [:db_insert_placeholder_3] => contact_details [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und [:db_insert_placeholder_6_uid] => 190 [:db_insert_placeholder_6_access] => 1 ) in field_sql_storage_field_storage_write() (line 451 of C:\XAMPP\htdocs\drupal\modules\field\modules\field_sql_storage\field_sql_storage.module).
0

Looks like Drupal is trying to set the delta column to the string "uid", which suggests you need another level in your array:

$user_perm[] = array (
  'uid'    => $account->uid,
  'access' => TRUE,
);

$ewrapper->field_user_permission->set($user_perm);
  • Unfortunately that threw another error, I managed to get it working without the entitymetadatawrapper but i can't get it working with it. – Dominic Woodman Sep 27 '13 at 15:20
0

I managed to get it working manually without the ewrapper. Using the old school method.

$entity->field_user_permission['und']['0']['uid'] = $account->uid;
$entity->field_user_permission['und']['0']['access'] = TRUE;
0

To set the initial value or additional values:

$ewrapper->field_user_permission[] = $user->uid;
$ewrapper->save();

This method also works with wrapped multi value Entity Reference fields.

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.