If storing entities in $_SESSION we can get following error:

( ! ) Fatal error: Call to undefined function entity_get_info() in /var/www/my_site/sites/all/modules/contrib/entity/includes/entity.inc on line 55
Call Stack
#       Time    Memory  Function        Location
1       0.0000  328312  {main}( )       ../index.php:0
2       0.0004  388768  drupal_bootstrap( )     ../index.php:20
3       0.0049  997056  drupal_session_initialize( )    ../bootstrap.inc:2188
4       0.0049  997732  drupal_session_start( ) ../session.inc:251
5       0.0049  997820  session_start ( )       ../session.inc:287
6       0.0064  1273024 Entity->__wakeup( )     ../entity.inc:0
7       0.0064  1273024 Entity->setUp( )        ../entity.inc:300

as we can see after calling session_start, php, tries to unserialize entity object, this automatically causes that __wakeup() is called and finally setUp( ) is called. setUp( ) has following code:


protected function setUp() {
    $this->entityInfo = entity_get_info($this->entityType);
    $this->idKey = $this->entityInfo['entity keys']['id'];
    $this->nameKey = isset($this->entityInfo['entity keys']['name']) ? $this->entityInfo['entity keys']['name'] : $this->idKey;
    $this->statusKey = empty($info['entity keys']['status']) ? 'status' : $info['entity keys']['status'];
  }

Just because

public function __sleep() {
    $vars = get_object_vars($this);
    unset($vars['entityInfo'], $vars['idKey'], $vars['nameKey'], $vars['statusKey']);
    // Also key the returned array with the variable names so the method may
    // be easily overridden and customized.
    return drupal_map_assoc(array_keys($vars));
  }

Not sure if there is any way to fix it, but it would be nice to have this fixed.

Comments

dealancer’s picture

Title: It is impossible to store entities in $_SESSION. » It is impossible to store entities in $_SESSION. Fatal error: Call to undefined function entity_get_info().
kevinquillen’s picture

I wound up serializing to get around this (it just happened to me over the weekend):

$_SESSION['solr']['query'] = serialize($this->query);

Didn't happen anymore. My purpose of storing an entity in session may be different than yours, though (saved Solr search params).

dealancer’s picture

well yeah, one of the solution is to stored serialized entities in the session and manually deserialize it.

kevinquillen’s picture

Yeah, was agreeing with you, I see what you're getting at. Wasn't sure if you knew/needed a quick workaround.

valthebald’s picture

I don't think there is an easy way to fix this, that will not affect performance.
Wakeup happens when Drupal is not bootstrapped yet, so it's impossible to use entity_get_property_info() or even drupal_get_path()