EntityMalformedException in custom module. Can anyone see what I did wrong?
Please check Examples, specifically entity_example module to check how to define the bundle.

So for Drupal 7 it goes like:

/**
 * Implements hook_entity_info().
 *
 * This is the fundamental description of the entity.
 *
 * It provides a single entity with a single bundle and without revision
 * support.
 */
function entity_example_entity_info() {
  $info['entity_example_basic'] = array(
    // A human readable label to identify our entity.
    'label' =&gt; t('Example Basic Entity'),

    // The controller for our Entity, extending the Drupal core controller.
    'controller class' =&gt; 'EntityExampleBasicController',

    // The table for this entity defined in hook_schema()
    'base table' =&gt; 'entity_example_basic',

    // Returns the uri elements of an entity.
    'uri callback' =&gt; 'entity_example_basic_uri',

    // IF fieldable == FALSE, we can't attach fields.
    'fieldable' =&gt; TRUE,

    // entity_keys tells the controller what database fields are used for key
    // functions. It is not required if we don't have bundles or revisions.
    // Here we do not support a revision, so that entity key is omitted.
    'entity keys' =&gt; array(
      // The 'id' (basic_id here) is the unique id.
      'id' =&gt; 'basic_id' ,
      // Bundle will be determined by the 'bundle_type' field.
      'bundle' =&gt; 'bundle_type',
    ),
    'bundle keys' =&gt; array(
      'bundle' =&gt; 'bundle_type',
    ),

    // FALSE disables caching. Caching functionality is handled by Drupal core.
    'static cache' =&gt; TRUE,

    // Bundles are alternative groups of fields or configuration
    // associated with a base entity type.
    'bundles' =&gt; array(
      'first_example_bundle' =&gt; array(
        'label' =&gt; 'First example bundle',
        // 'admin' key is used by the Field UI to provide field and
        // display UI pages.
        'admin' =&gt; array(
          'path' =&gt; 'admin/structure/entity_example_basic/manage',
          'access arguments' =&gt; array('administer entity_example_basic entities'),
        ),
      ),
    ),
    // View modes allow entities to be displayed differently based on context.
    // As a demonstration we'll support "Tweaky", but we could have and support
    // multiple display modes.
    'view modes' =&gt; array(
      'tweaky' =&gt; array(
        'label' =&gt; t('Tweaky'),
        'custom settings' =&gt; FALSE,
      ),
    ),
  );

  return $info;
}


Or for simpler example, entity.api.php from Entity module which goes like:

function entity_crud_hook_entity_info() {
  $return = array(
    'entity_test' =&gt; array(
      'label' =&gt; t('Test Entity'),
      'entity class' =&gt; 'Entity',
      'controller class' =&gt; 'EntityAPIController',
      'base table' =&gt; 'entity_test',
      'module' =&gt; 'entity_test',
      'fieldable' =&gt; TRUE,
      'entity keys' =&gt; array(
        'id' =&gt; 'pid',
        'name' =&gt; 'name',
        'bundle' =&gt; 'type',
      ),
      'bundles' =&gt; array(),
    ),
  );
  foreach (entity_test_get_types() as $name =&gt; $info) {
    $return['entity_test']['bundles'][$name] = array(
      'label' =&gt; $info['label'],
    );
  }
  return $return;
}


The error EntityMalformedException which you're having it's saying that your bundle property is malformed on save, so Drupal core can't identify what type (bundle) is that.

Troubleshooting:


Check what's the value of bundle in entity keys section of array defined in entity information array via:

drush eval 'print_r(entity_get_info("entity_type"));'

If it's type, then that means your entity which you're trying to save, it's missing type attribute.


The common mistake is usually to load the entity via entity_load() and trying to re-save it, but remember that return value is an array of entity objects indexed by their ids.

So the simple test to verify if your entity re-saving works correctly, it would be:

drush eval '$entity = entity_load("entity_type", array(1)); entity_get_controller("entity_type")-&gt;save(reset($entity))'


where entity_type is your entity_type and 1 is your entity id.