0

I have a custom module which defines a custom entity. Now I would like to add a new field to this entity without reinstalling my module. I tried it with the hook_update_N() function:

/**
 * Description
 */
function MY_MODULE_update_7100() {
  $spec = array(
    'type' => 'int',
    'description' => 'Description',
    'not null' => FALSE,
  );
  db_add_field('my_table', 'my_new_field', $spec);
}

in my Module, I updated my existing hook_entity_property_info_alter to add the definition for this field:

function MY_MODULE_entity_property_info_alter(&$info) {
  $props = &$info['my_entity']['properties'];
  // snip
  $props['my_new_field'] = array(
    'label' => t('My Label'),
    'type' => 'integer',
    'description' => t('My Description'),
    'schema field' => 'my_new_field',
  );
  // snap
}

After updating, I can still create and update the entity, but sadly not my new Field which I created.

What did I wrong?

I looked for an example in the commerce module. But the module is using drupal_write_record instead of entity_save.

PS: I already saw this issue but it didn't worked.

1 Answer 1

6

I've had this problem myself in the past and almost pulled all my hair out! :)

It turned out that I had forgotten to update my hook_schema declaration within the .install file of my module, and include the definition of the new database field for my entity table.

Adding in the definition should make the new field available to you.

6
  • Well, I'm not sure that I understand you correctly. If I update my hook_schema() function, I have to reinstall the module and loose all my entries to update my table. The new Field for the database was added successfully after updating. But it seems to be that the entity does not understand that there is a new Field to fill in the Table.
    – moertle
    Commented Feb 5, 2014 at 9:54
  • 2
    @moertle you just need to add the new field to your hook_schema definition. It's referred to when updating your entity. Commented Feb 5, 2014 at 10:01
  • Now it works! Both, Thank you very much, you saved my day! Heroes! :)
    – moertle
    Commented Feb 5, 2014 at 10:14
  • no problem! :) I also just recently started working with custom entitites and know how confusing it can initially be. Commented Feb 5, 2014 at 12:06
  • ohh!!! you are just awesome....... after 1 day of search i just found this solution +1
    – Lucifer
    Commented Jan 28, 2015 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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