Is this a good way to make fields accessible for an empty entity object? I have a function, my_entity_create(), which returns an empty object for my entity with properties: isnew, name, etc...

I want my fields attached to this empty object so I can set their values.  I accomplished this by calling the function below and adding the results to my object in my_entity_create().  Now I can set $my_entity_obj->field_my_field['und'][0]['value'] = 'some value'.  I save it with entity_save() and it works.

My question:  is there a better way to do this?  I'm trying to learn to do things 'the drupal way'... or just the best way if there is not a drupal best practice for a certain task.

function _get_fields_for_bundle($bundle)
{
    $result = db_select('field_config_instance', 'f')
        -&gt;fields('f', array('field_name'))
        -&gt;condition('entity_type', 'my_entity_type')
        -&gt;condition('bundle', $bundle)
        -&gt;execute()
        -&gt;fetchAllAssoc('field_name');

        return array_keys($result);
}
I too was looking for an answer to this question, however you question has provided me with the best answer so far.

My implementation of your solution was to call your function in my entity create function and then loop over the values attaching the fields as empty arrays:

function MY_ENTITY_create($values = array()) {
    $entity = entity_get_controller('MY_ENTITY')-&gt;create($values);
    foreach(_get_fields_for_entity('MY_ENTITY') as $field) {
        $entity-&gt;{$field} = array();
    }
    return $emtr_safe_check;
}

function _get_fields_for_entity($entity) {
    $result = db_select('field_config_instance', 'f')
        -&gt;fields('f', array('field_name'))
        -&gt;condition('entity_type', $entity)
        -&gt;condition('bundle', $entity)
        -&gt;execute()
        -&gt;fetchAllAssoc('field_name');

        return array_keys($result);
}


As my entity does not have a bundle, but my module does define several different entities I modified your function slightly to handle loading fields for different entities with bundles of the same name.

I would still be interested to see other peoples solution to this problem.