2

I have defined my own entity: 'game'; created some 'games' with entity_create; and loaded one of these 'games' into a form for editing. Now, I want to update the entity with my submit handler. Here is the relevant code inside the handler:

$game = entity_load_single('game', $form_state['values']['game_id']);
$wrapper = entity_metadata_wrapper('game', $game);
$wrapper->some_property->set($form_state['values']['some_value']);
$wrapper->save();

This throws:

Error

EntityMetadataWrapperException: Entity property some_property doesn't support writing. in EntityStructureWrapper->setProperty() (line 493 of /.../sites/all/modules/contrib/entity/includes/entity.wrapper.inc).

I was looking for a function like entity_update, but it doesn't seem to exist. How can I update my 'game' entities? Thanks.

1

You're probably looking for entity_save().

If you want to use a metadata wrapper for a custom entity properties you need to implement hook_entity_property_info() to describe them to the entity module, especially setter callbacks which are used to set the value of the property.

0

If you created your entity via entity_create (as you mentioned), you can set/override non-writable properties outside of the entity wrapper during the entity creation, e.g.

$entity = entity_create('games',
    array(                              
      'type' => 'game', 
      'changed' => REQUEST_TIME,     
      'some_property' => 'some value',        
    ));                                 
$wrapper = entity_metadata_wrapper('game', $entity);  
$wrapper->save(); 

Otherwise you'll need to write your own setter callback as already suggested.

See also: How to fix entity property X doesn't support writing, when setting content type field values?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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