Problem

Typed entities like MyEntity are not imported.

Analysis

I have created a Statistic entity based on Model module. Basically I have searched and replaced the string "model" with string "statistic" preserving the case.

In this module the arguments in entity methods are typed:

function statistic_save(Statistic $statistic) {
  return $statistic->save();
}

In feed_import/feed_import.inc.php function createEntity entities are created as stdClass like this:

  $entity = new stdClass();

This causes an error:
Argument 1 passed to statistic_save() must be an instance of Statistic, instance of stdClass given

...and entities are not imported.

Failed attempts to solve the problem

I tried to remove the typing from the entity method arguments like this:

function statistic_save($statistic) {
  return $statistic->save();
}

I get this PHP error:
Fatal error: Call to undefined method stdClass::save()

Suggestion

I managed to solve the issue by adding this to feed_import/feed_import.inc.php function createEntity:

  public static function createEntity(&$feed, &$item) {
    // Create new object to hold fields values.
    if($feed['entity_info']['#entity']) {
      //Ensure camel casing. Example from 'my_entity' to 'MyEntity'.
      $entity_class_parts = explode('_', $feed['entity_info']['#entity']);
      $entity_class = '';
      foreach ($entity_class_parts as $part) {
        $entity_class .= ucfirst($part);
      }
      $entity = new $entity_class;
    } else {
      $entity = new stdClass();
    }

I doubt this will work if the entity is defined as stdClass. Also it's not foolproof if somebody names the entity differently when using the underscore name and the camel case name.

Is there any better way to approach this?

Comments

jiv_e’s picture

I found a way out of this by using entity_save function of the entity module directly.

function statistic_save($statistic) {
  return entity_save('statistic',$statistic);
}

I'll have to file an issue to model module's issue queue.

jiv_e’s picture

Status: Active » Closed (works as designed)
jiv_e’s picture

Issue summary: View changes