如何在会话到期后修复Zend DbTableGateway读取/销毁无限循环?
I have a Zend\Session Manager with a DbTableGateway to handle sessions. My code is in another SO answer. But whenever the session expires, I get an error Fatal error: Maximum function nesting level of '100' reached, aborting! in [path]\vendor\zendframework\zend-db\src\Sql\AbstractExpression.php on line 40
I found out how to change the xdebug nesting level to 200, but then I reached the 200 limit too.
So I have an infinite loop between DbTableGateway->read() and DbTableGateway->destroy(). I noticed the 2 lines referenced in the error each call the other method, but it's Zend code so I don't want to change it.
It only happens after the session expires, and not when I remove the row containing the session information from the database. When I looked at the table after this occurred, I noticed that the row is still present in the table except for the data column.
id name modified lifetime data
[random string] [session name] 1468587768 1440 -
So it looks like the read() method checks the table for a row containing the id and name, and finds this row, then calls destroy(), which calls read(). How can I fix this?
On a side note, refreshing the page solves the problem for the user, but I don't want to have code that throws an exception at the user and forces them to refresh the page.
This worked for me: create a class that extends DbTableGateway and modify its destroy method to check if a row exists rather than call the read() function. Then use that class instead of DbTableGateway.
So here's the code for the new save handler class:
class MySessionSaveHandler extends DbTableGateway
{
/**
* Destroy session
*
* @param string $id
* @return bool
*/
public function destroy($id)
{
// I removed "read" to prevent an infinite loop, and am using the first line of "read" instead to check if the row exists
$rows = $this->tableGateway->select([
$this->options->getIdColumn() => $id,
$this->options->getNameColumn() => $this->sessionName,
]);
if (! (bool) $rows) {
return true;
}
return (bool) $this->tableGateway->delete([
$this->options->getIdColumn() => $id,
$this->options->getNameColumn() => $this->sessionName,
]);
}
}
And here's how you replace DbTableGateway:
/* @var $adapter \Zend\Db\Adapter\Adapter */
$adapter = $serviceManager->get('Zend\Db\Adapter\Adapter');
$tableGateway = new TableGateway('mytablename', $adapter);
return new MySessionSaveHandler($tableGateway, new DbTableGatewayOptions());