在用户会话中存储主义记录

问题描述

| 因此,我有一个可根据用户ID为我生成信息的插件。 这发生在插件的模块'pitch \'中:
public function executeIndex(sfWebRequest $request)
{
  $unique_id = $request->getParameter(\'unique_id\');
  $this->user = UserTable::getInstance()->getByToken($unique_id);
  $this->forward404Unless($this->user);
  $this->iplocation=new IPLocation();

  $qualified_offers = new QualifiedOffers();
  $this->creatives = $qualified_offers->applicableTo($this->user);
  $this->match_type = UserDemoTable::getInstance()->getValue($this->user->id,\'match\');

  // Put the applicable creatives into the session for later use
  $userCreatives = $this->creatives;
  $this->getUser()->setAttribute(\'userCreatives\',$userCreatives);
}
然后,我尝试在后续模板上调用该属性(在另一个名为\'home \'的模块中,执行不同的操作):
public function executePage(sfWebRequest $request)
{
  $template = $this->findTemplate($request->getParameter(\'view\'),$this->getUser()->getCulture());
  $this->forward404Unless($template);
  $this->setTemplate($template);
  // Grab the creatives applicable to the user
  $userCreatives = $this->getUser()->getAttribute( \'userCreatives\' );
}
不幸的是,它根本不起作用。 如果我从最初生成$ creatives的操作中尝试这样做:
$this->getUser()->setAttribute(\'userCreatives\',$userCreatives);
$foo = $this->getUser()->getAttribute(\'userCreatives\');
// Yee haw
print_r($foo);
我获得了巨大的成功。我实际上是从两个不同的控制器执行此操作。考虑到我已经向用户会话添加了\\\\\\\\\\\\\\\\\\\\\\\\\ CreativeCreatives \\\ CreativeCreative \\\\ Creative \\\\\\\\\\ Creative \\\\\\\\ Creative \\\\\\ Creative \\\ Creative \\\\\\ CreativeCreative \\\\\\\\\ CreativeCreative \\\\\\\\ CreativeCreative \\\\\\\\ CreativeCreative \\\\\\\\\ CreativeCreative \\     

解决方法

        听起来您正在尝试将对象存储为用户属性(即在会话中)。 从Jobeet第13天开始:   您可以在用户会话中存储对象,但是强烈建议不要这样做。这是因为会话对象在请求之间被序列化。反序列化会话时,必须已经加载了存储对象的类,但并非总是如此。此外,如果您存储Propel或Doctrine对象,则可能有\“ stalled \\”对象。 尝试存储对象的
array
stdClass
表示,然后在检索到它们之后将它们重新加载到\“ full \”对象中。 这是我在另一个项目上使用的示例:
class myUser extends sfGuardSecurityUser
{
  ...

  public function setAttribute( $name,$var )
  {
    if( $var instanceof Doctrine_Record )
    {
      $var = array(
          \'__class\'  => get_class($var),\'__fields\' => $var->toArray(true)
      );
    }

    return parent::setAttribute($name,$var);
  }

  public function getAttribute( $name,$default )
  {
    $val = parent::getAttribute($name,$default);

    if( is_array($val) and isset($val[\'__class\'],$val[\'__fields\']) )
    {
      $class  = $val[\'__class\'];
      $fields = $val[\'__fields\'];

      $val = new $class();
      $val->fromArray($fields,true)
    }

    return $val;
  }

  ...
}
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...