会话过期后如何修复 Zend DbTableGateway read/destroy 无限循环?
How to fix Zend DbTableGateway read/destroy infinite loop after session expiration?
我有一个 Zend\Session 带有 DbTableGateway 的管理器来处理会话。我的代码在 中。但是每当会话过期时,我都会收到错误 Fatal error: Maximum function nesting level of '100' reached, aborting! in [path]\vendor\zendframework\zend-db\src\Sql\AbstractExpression.php on line 40
我找到了如何将 xdebug 嵌套级别更改为 200,但后来我也达到了 200 的限制。
所以我在 DbTableGateway->read() 和 DbTableGateway->destroy() 之间有一个无限循环。我注意到错误中引用的 2 行每行都调用了另一个方法,但它是 Zend 代码,所以我不想更改它。
它只发生在会话过期之后,而不是当我从数据库中删除包含会话信息的行时。发生这种情况后,当我查看 table 时,我注意到该行仍然存在于 table 中,但数据列除外。
id name modified lifetime data
[random string] [session name] 1468587768 1440 -
所以看起来 read() 方法在 table 中检查包含 id 和名称的行,并找到这一行,然后调用 destroy(),后者调用 read()。我该如何解决这个问题?
附带说明一下,刷新页面可以解决用户的问题,但我不想让代码向用户抛出异常并强制他们刷新页面。
这对我有用:创建一个扩展 DbTableGateway 的 class 并修改其 destroy 方法以检查行是否存在而不是调用 read() 函数。然后使用 class 而不是 DbTableGateway。
这是新保存处理程序的代码 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,
]);
}
}
下面是替换 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());
我有一个 Zend\Session 带有 DbTableGateway 的管理器来处理会话。我的代码在 Fatal error: Maximum function nesting level of '100' reached, aborting! in [path]\vendor\zendframework\zend-db\src\Sql\AbstractExpression.php on line 40
我找到了如何将 xdebug 嵌套级别更改为 200,但后来我也达到了 200 的限制。
所以我在 DbTableGateway->read() 和 DbTableGateway->destroy() 之间有一个无限循环。我注意到错误中引用的 2 行每行都调用了另一个方法,但它是 Zend 代码,所以我不想更改它。
它只发生在会话过期之后,而不是当我从数据库中删除包含会话信息的行时。发生这种情况后,当我查看 table 时,我注意到该行仍然存在于 table 中,但数据列除外。
id name modified lifetime data
[random string] [session name] 1468587768 1440 -
所以看起来 read() 方法在 table 中检查包含 id 和名称的行,并找到这一行,然后调用 destroy(),后者调用 read()。我该如何解决这个问题?
附带说明一下,刷新页面可以解决用户的问题,但我不想让代码向用户抛出异常并强制他们刷新页面。
这对我有用:创建一个扩展 DbTableGateway 的 class 并修改其 destroy 方法以检查行是否存在而不是调用 read() 函数。然后使用 class 而不是 DbTableGateway。
这是新保存处理程序的代码 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,
]);
}
}
下面是替换 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());