Custom entity, entity_save() leads to PDO Exception? I have built a custom entity called 'person'.

In a module called 'beautify' I use a hook.

Inside this hook I want to create an instance of this object with bundle 'person'. (entity: 'person', bundle: 'person')

In my bundle person I have different fields created by the fields UI. One of these fields is called 'field_person_copy'. The colums of this table called 'fields_data_fields_person_copy' are created by the drupal system:
entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_copy_person_value.
The field is required.

I want to use the drupal system to create new persons, not a db_inject.

  function beautify_contact_person($message){
  // dpm($message);
  $e = entity_create ('person', array(
    $entity-&gt;name = $message['params']['last_name'],
    $entity-&gt;field_person_copy['und'][0]['value'] = 'NO',
    ));
  //dpm($e,'Instanziiert eine entity');
  entity_save('person', $e);


(Testing with entity->save let the system say that this method is undefined.)

This leads to the following error message:

  PDOException: SQLSTATE[23000]: Integrity constraint violation: 
  1048 Column 'entity_id' cannot be null: INSERT INTO {field_data_field_person_copy} 
  (entity_type, entity_id, revision_id, bundle, delta, language, field_person_copy_value) 
  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); Array ( [:db_insert_placeholder_0] =&gt; 
 person[:db_insert_placeholder_1] =&gt; [:db_insert_placeholder_2] =&gt; [:db_insert_placeholder_3] 
 =&gt; person [:db_insert_placeholder_4] =&gt; 0 [:db_insert_placeholder_5] =&gt; und 
[:db_insert_placeholder_6] =&gt; NO ) in  field_sql_storage_field_storage_write() 
(Line 451 von /home/duke/sites/mia-casa/modules/field/modules/field_sql_storage/field_sql_storage.module).


which means that the system doesn't generate the entity_id and fuels it in the function.

So is that a bug of the entity_api or from me? (I hope the latter) ;)

In my custom_module to build the entity, I only overwrote the function "buildContent" in the class EntityApiController, so create, save and update is not influenced by me.

Thx in advance,

maen
The problem is centred around this bit of code:

$e = entity_create ('person', array(
  $entity-&gt;name = $message['params']['last_name'],
  $entity-&gt;field_person_copy['und'][0]['value'] = 'NO',
));


It's sort-of-technically-valid PHP (though you don't have a variable named $entity in scope), but certainly isn't doing what you expect. It's almost like you're trying to apply a feature from another language (like object initialisers from C# maybe).

I'm guessing somewhat, but I imagine what you're trying to do is:

$e = entity_create ('person', array(
  'BUNDLE_KEY' =&gt; 'BUNDLE_NAME',
  'name' =&gt; $message['params']['last_name'],
));

$e-&gt;field_person_copy[LANGUAGE_NONE][0]['value'] = 'NO';

entity_save('person', $e);


Or maybe

$e = entity_create ('person', array(
  'BUNDLE_KEY' =&gt; 'BUNDLE_NAME',
));

$e-&gt;name = $message['params']['last_name'];
$e-&gt;field_person_copy[LANGUAGE_NONE][0]['value'] = 'NO';

entity_save('person', $e);


Where BUNDLE_KEY and BUNDLE_NAME are whatever you have set up for your entity type.