如何解决在用户会话中存储主义记录
| 因此,我有一个可根据用户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;
}
...
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。