CodeIgniter Forums
Invalid serialization data for DateTime object - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Invalid serialization data for DateTime object (/showthread.php?tid=77393)



Invalid serialization data for DateTime object - AngelRodriguez - 08-26-2020

Hi,

I create an entity and cache it. When I try to get from cache it gives me error: "Invalid serialization data for DateTime object".

This is example code:

Code:
$web = new \App\Entities\Web();
$web->created_at = date('Y-m-d H:i:s');
$cache->save('webname', $web, 300);

$web = $cache->get('webname');
if ($web) {
    echo 'cached';
    printr($web);
}

Where is the problem? How can fix this?

Thank you,

Angel


RE: Invalid serialization data for DateTime object - Chroma - 08-27-2020

Rather than saving the DateTime as a string, have you considered saving it as an integer, that is the number of seconds, as you are getting from time()?

That would remove the parsing problem and make it easier to transform the date into something easier to process. You can always turn a time() into a DateTime.


RE: Invalid serialization data for DateTime object - AngelRodriguez - 08-27-2020

(08-27-2020, 04:22 AM)Chroma Wrote: Rather than saving the DateTime as a string, have you considered saving it as an integer, that is the number of seconds, as you are getting from time()?

That would remove the parsing problem and make it easier to transform the date into something easier to process. You can always turn a time() into a DateTime.

Yea, it could be a solution. But I prefer to solve the problem because I think is a Codeigniter problem.

Thank you.