f3-factory / fatfree-core

Fat-Free Framework core library
GNU General Public License v3.0
207 stars 88 forks source link

php session with SQL\Session / reroute() and rollback() #302

Open kumy opened 4 years ago

kumy commented 4 years ago

I took me some time to find why my newly added sessions values where not saved in database when using reroute(), so I thought this might need some note in the documentation.

In my context I was adding some flash messages in the SESSION while I was in a transaction. When something wrong happen, I issued reroute() (without explicitly issuing a rollback() - and db server auto-commit off) and then lose the fresh session values.

Basic code would be:

<?php
$db = $f3->get('DB');
new \SQL\Session($db);
$db->begin();
$f3->set('SESSION.flag', true);
// Do something…

// $db->rollback();  // Without this, the session values are also reverted.
$f3->reroute('…');

// On next page, the `SESSION.flag` should be absent :/

It could be easy to forget the rollback() call, this could probably be written somewhere in the documentation? :)