0

I've a custom entity {my_table} and added a new field in schema file as follows

/**
 * Add {new_column} field to {my_table} table.
 */
function MY_MODULE_update_7100() {
    $table = 'my_table';
  $spec = array(
    'type' => 'int',
    'description' => "blah blah",
    'size'=> 'tiny',
    'default' => NULL,
  ); 
  db_add_field($table, 'new_column', $spec);
}

When I ran update.php the database table column got updated.

But the problem is when I run 'entity_load' it's not fetching the added column to the object.

$get_entity = entity_load('{my_table}', array(184));
dpm($get_entity) // showing all columns, except the new_column

I've cleared the cache but no luck. Any suggestions would be much appreciated.

2
  • What does the entity definition look like? That is where you need to also change things, in order for the database changes to reflect a change to your entity's model. Commented Nov 19, 2018 at 16:31
  • Oh yeah! how could I missed that logic. Thanks @StefanosPetrakis. When you get time please add that as an answer. I'm done with this :) thanks again Commented Nov 19, 2018 at 16:35

1 Answer 1

1

If I have understood you correctly, as well as updating the table you must provide an updated schema definition for the entity. Drupal uses this definition to determine the names and properties of the columns in the entity.

Note you do not need to do an 'update' action for the schema definition: just change it to match the current code. You (only) need the update hook for databases that enabled the earlier version of the module. Also, importantly, the update hook must both add the column to the database, but set the new property value for any rows appropriately. Often this can be done by the database default value, but not always.

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.