CRUDing Entity Properties with Entity Metadata Wrappers

In D7 Entity, EFQ (Entity Field Query) is a nice one for manipulating the Entity Fields. At some points, we might need to use Entity Properties to CRUD if we don't want to use Fieldable entity for certain reasons. Personally I just found two approaches after trials and errors because there is no documentation/article about updating Entity Properties.

1) Traditional db_query/db_merge.

  • It is first initially.
  • But we have to know detail table structures every time we want to update the queries.

2) Using "Entity metadata wrappers", its set() and save() methods.

  • Some extra setup are needed initially,
  • Very easy to update in future without knowing column names of the base table.
  • Views support

If we want to use "Entity metadata wrappers", we need to implement some more steps after hook_entity_info().
1) Define hook_entity_property_info() for the entity.
2) Define
   'setter callback' => 'entity_property_verbatim_set' and
   'getter callback' => 'entity_property_verbatim_get' for each property we want to RUD.

Then we can use set() and save() methods of Entity metadata wrappers

The following is the abstract example and I hope some of the community members will be useful in future without Googling elsewhere..

function hook_entity_info(){
    your entity info definitions in array.

function hook_entity_property_info(){
    $info = array();
    $properties = &$info['custom_entity_name']['properties'];

    -- some property definitions here..

    $properties['property_one'] = array(
        'label' => t('Property One'),
        'description' => t('The description of the property one.'),
        'setter callback' => 'entity_property_verbatim_set', // To update/add new values
        'getter callback' => 'entity_property_verbatim_get', // To get/read values
        'type' => 'data_type',
        'schema field' => 'column_name_xx_in_base_table',

    -- some other property definitions here..

    return $info;

Example: CRUDing Entity Properties using "Entity metadata wrappers"

    // Get the properties we defined in hook_entity_property_info()
    $wrapper = entity_metadata_wrapper($entity_name, $entity_id);

    // Read/Get the value of the entity property using value()

    // property_one is column_name_xx_in_base_table as we defined above.
    // So, we don't really need to know base table name, columns etc.

    // Update/Set and Save using set() and save().
    $wrapper->property_one->set('New Value');

    // Delete set(NULL)

We can't use set() and save() methods without setting 'setter callback' and 'getter callback' to the properties. That is the important point if we want to CRUD entity properties.

More info about EntityMetadatWrapper -

I hope some of the members will find this article, useful in future without Googling elsewhere and wasting time.

Please feel free to update/comment/correct with better/alternative approahes, links and idea for me and other community members.
Thanks for reading such a long post..

Add new comment

Featured Articles