9

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?

3 Answers 3

15

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.

2
  • Think about it: If you store all that entity, you will store a Big object in your session data. For your develepment enviroment, it is not a problem. But if you have a lot of users, it is a waste of resources. The solution is so simple: store the id of the object and use the find() method of the repository to retrieve the object. At the end, it is the same thing. Apr 14, 2012 at 15:56
  • Well, I think, the framework should provide this function to me. So I dont have to call find function every time I want to use the entity. Apr 14, 2012 at 18:03
6

Serializing entities is not recommended, as you can see in the Doctrine-documentation. You should implement the Serializable-interface and serialize/deserialize the entity-data manually.

3
  • Serializing my entity would be problematic as it contains many foreign objects and even ArrayCollections of foreign entities. Any better idea than only storing the id and retrieving the object from the database every time I need it? Feb 21, 2012 at 21:06
  • As you can see from your exception, when putting it into a session it will be serialized, that is what's causing the error. But as you say, due to the complexity of your object, you should probably go with storing the id and retrieve the object from the database again. Possibly using some kind of object cache could work as well, but I am not sure.
    – dbrumann
    Feb 22, 2012 at 8:28
  • 1
    Thank you, I'm only going to store the id. Feb 27, 2012 at 12:22
2

You can exclude unnesseary fields by overridding __sleep method:

public function __sleep() {

    // these are field names to be serialized, others will be excluded
    // but note that you have to fill other field values by your own
    return array('id', 'username', 'password', 'salt');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.