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

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

问题描述

我想将我的一个实体对象保存到会话中,但是在执行此操作时,出现以下两个错误:

例外: Symfony \ Bundle \ FrameworkBundle \ DataCollector \ RequestDataCollector :: serialize() 必须返回字符串或NULL

ErrorException:注意: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关系) ,它将无法正常工作.

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

这会引发一个错误,因为优惠券具有一个对象属性,根据EntityManager的说法,该属性不在数据库中(实际上是在数据库中,我从数据库中放入了会话).

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

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

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

解决方案

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

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

app/console cache:clear

即使您所说的它包含许多外来对象,甚至包含外来实体的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: Symfony\Bundle\FrameworkBundle\DataCollector\RequestDataCollector::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天全站免登陆