We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Exception: Serialization of 'Closure' is not allowed

Hi, i need put a object Phalcon\Mvc\Model\Query\Builder into the session, but i receive a exception: Exception: Serialization of 'Closure' is not allowed! Can i resolve this problem someway?

Thanks

No, why you even want to put it into session ?



125.7k
Accepted
answer

This can't be resolved because the object has an anonymous function (otherwise known as a closure) and they cannot be serialized.

The right question was asked - why even put it in a session? The Builder itself doesn't store the results of the query, so you're not saving a database trip (or trips). Sure, storing the Builder in Session would prevent it from being built repeatedly, but with Phalcon, the amount of processor cycles that takes is so minimal, it's practically a non-issue for the vast majority of sites.

Dylan anderson - you forgot about serializing/unserializing, even if it could be stored in session - the difference would be none or even it would be slower. The REAL difference could be make if we could have REAL FASTCGI in php - like we have whole application started after starting server - and all requests are parsed by it and response is returned - but application is never rebuiled, just response is cleaned and waiting for another requests. There is such a project for php which speed it up by really majority of time but it has a lot of issues.

@Jurigag: PHP has FPM. What you're talking about is actual application cache - and such thing perhaps can be achivied with i.e. Memcached. But let's focus on question here - putting an object Phalcon\Mvc\Model\Query\Builder into the session is just NO NO.

No. Im not talking about application cache. You can't achieved it with memcached. Im talking about application just iniciated once, and it's listening for requests and returning responses. Either FPM or Memcache can git it to you. I'm talking about this - https://github.com/PHPFastCGI

You're talking about concept - application as a daemon. OK that's another topic.

Yea, exactly. This is really what fastcgi should be.

I have a application that use query/builder for create reports dynamics, i send the query builder in my controller and in another file that is called with asynchronous request that run the query, but i will use another approach in my application for resolve this problem!

Thanks so much!

Then store query or resultset in cache or something,