0

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->name = $message['params']['last_name'],
    $entity->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] => 
 person[:db_insert_placeholder_1] => [:db_insert_placeholder_2] => [:db_insert_placeholder_3] 
 => person [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => und 
[:db_insert_placeholder_6] => 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

closed as off-topic by kenorb, Scott Joudry, Krishna Mohan, Adrian Cid Almaguer, Mołot Feb 4 '16 at 22:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced, was solved by a cache clear, or was a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." – kenorb, Scott Joudry, Adrian Cid Almaguer
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    There's no $entity variable declared in your code, where does that come from? And why are you assigning to properties of $entity in array keys?? – Clive Dec 23 '14 at 15:31
  • I don't need to I believe: if you look at drupalcontrib.org/api/drupal/… the line_item – maen Dec 23 '14 at 16:25
  • Also: The error says that drupal uses the correct table! So the field is recognized in my mind!!!??? – maen Dec 23 '14 at 16:30
  • ??? Don't need to what? A quick search on google will find you the correct way to create an entity. BTW, you are also not giving it a bundle type. – 2pha Dec 23 '14 at 16:30
  • 1
    Your code doesn't match the code you've linked to, in any way. I genuinely don't mean this to sound rude: how well do you know PHP? There seems to be a knowledge-gap here. While what you have is technically valid syntax, I can all but guarantee you it's not doing what you think it is – Clive Dec 23 '14 at 16:40
1

The problem is centred around this bit of code:

$e = entity_create ('person', array(
  $entity->name = $message['params']['last_name'],
  $entity->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' => 'BUNDLE_NAME',
  'name' => $message['params']['last_name'],
));

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

entity_save('person', $e);

Or maybe

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

$e->name = $message['params']['last_name'];
$e->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.

0

Thx for your input! All of you, because your criticism helped me a lot!

I could solve it like this:

function beautify_contact_person($message){
$e = entity_create ('person', array(
    'name' => $message['params']['last_name'],
    'prename' => $message['params']['name'],
    'customer' => 0,
    'type' => 'person',
    'field_person_copy' => array(
        LANGUAGE_NONE => array(
            0 => array(
                'value' => 'NO',
            ) 
        )
    ),
    'field_person_tel_privat' => array(
        LANGUAGE_NONE => array(
            0 => array(
                'value' => $message['params']['phone'],
            ) 
        )
    ),... `
 )->save();  
}

It was the 'type' that irritates me ...

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