Symfony2 将实体对象序列化为会话 [英] Symfony2 serialize entity object to session

查看:21
本文介绍了Symfony2 将实体对象序列化为会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的实体对象之一保存到会话中,但在这样做时,我收到以下两个错误:

<块引用>

异常:SymfonyBundleFrameworkBundleDataCollectorRequestDataCollector::serialize()必须返回一个字符串或NULL

<块引用>

ErrorException: Notice: serialize(): "id" 作为成员返回来自 __sleep() 的变量但不存在于/var/www/clients/client71/web256/web/_dev_fd/kkupon/vendor/symfony/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php第 29 行

我的代码是这样的:

$offer = $this->getEntityManager()->getRepository('KkuponMainBundle:Offer')->find($offer_id);$request->getSession()->set('offer', $offer);

我怎么做对?

谢谢.

更新在 Rowgm 的帮助下,我可以通过将属性设置为 protected 而不是 private 来解决这个问题.我唯一的问题是在读取会话中的实体后,EntityManager 不知道它,如果我将对象(来自会话)添加到另一个对象(它们之间存在 OneToMany 关系),它不会工作.

get('session')->get('offer');$优惠券=新优惠券();$coupon->setOffer($offer);$this->em->persist($coupon);$this->em->flush();

这引发了一个错误,因为coupon有一个对象属性,根据EntityManager它不在数据库中(实际上它在数据库中,我从数据库放到会话中).

get('session')->get('offer');echo $this->em->getUnitOfWork()->isInIdentityMap($offer) ?是":否";//结果:没有

一种解决方案可以是:$offer = $this->em->merge($offer);

但这似乎不是最好的.我希望我的 EntityManager 能够感知存储在会话中的实体对象,而无需每次都告诉它.有什么想法吗?

解决方案

您可以通过将任何实体的所有属性和关系从 private 设置为 protected 来序列化任何实体.>

symfony2 可能有一个常见问题,即使您已将所有属性设置为受保护:您必须重新生成已更改实体的代理.为此,只需清除缓存即可.对于开发环境:

应用/控制台缓存:清除

即使它包含许多外来对象,甚至是外来实体的 ArrayCollections",它也能正常工作.

I want to save one of my entity objects into the session, but as I'm doing so, I'm getting the following two errors:

Exception: SymfonyBundleFrameworkBundleDataCollectorRequestDataCollector::serialize() must return a string or NULL

and

ErrorException: Notice: serialize(): "id" returned as member variable from __sleep() but does not exist in /var/www/clients/client71/web256/web/_dev_fd/kkupon/vendor/symfony/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php line 29

My code goes like this:

$offer = $this->getEntityManager()->getRepository('KkuponMainBundle:Offer')->find($offer_id);
$request->getSession()->set('offer', $offer);

How could I get it right?

Thank you.

UPDATE With Rowgm's help I could fix this problem by setting properties protected instead of private. The only problem I have is after reading the entity from the session the EntityManager does not know about it, and if I add the object(from the session) to another object(there is OneToMany relationship between them), it won't work.

<?php
$offer = $this->get('session')->get('offer');
$coupon = new Coupon();
$coupon->setOffer($offer);
$this->em->persist($coupon);
$this->em->flush();

This raises an error, because coupon has an object property which according to the EntityManager is not in the database(actually it is in the DB, I put to the session from the DB).

<?php
$offer = $this->get('session')->get('offer');
echo $this->em->getUnitOfWork()->isInIdentityMap($offer) ? "yes":"no"; //result: no

One solution can be: $offer = $this->em->merge($offer);

But this doesnt seem to be the best one. I'd like my EntityManager to perceive entity objects stored in session without telling it each time. Any idea?

解决方案

You can serialize any entity by setting all their properties and relationships from private to protected.

You could have a common issue with symfony2, even if you have set all properties to protected: You have to re-generate the proxies of those entities you have changed. To do so, simply clear the cache. For dev enviroment:

app/console cache:clear

It works even if "it contains many foreign objects and even ArrayCollections of foreign entities" as you said.

这篇关于Symfony2 将实体对象序列化为会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆