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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…