Drupal - Create Entity Programmatically in Drupal

The simpler approach is with the help of entity_metadata_wrapper().

$e = entity_create($entityType, array('type' => $bundleName));
$ew = entity_metadata_wrapper($entityType, $e);
$ew->field_name->set('value');
$ew->save();

Most of what you have doesn't look too bad. The key thing with fields that you are missing is that they have a language, that they can be multi-valued, and that the value is generally stored in a 'value' subkey. I'm assuming that the ref field is a nodereference, which means it's actually stored with nid instead of value.

The easiest thing you can do to see how this is structured is to install the Devel module, and use the Devel tab that to look at the structure. Not sure if that shows up with ECK entities, but it does for Nodes.

Try this:

$new_ent = entity_create('receipt', array(
  'uid' => 1,
  'type' => 'receipt', //bundle
  'field_receipt_prof_ref' => array(      
    LANGUAGE_NONE => array(
      0 => array(
        'nid' => 1,
      ),
    ),
  ),
))->save();

Tags:

Entities

7