我正在使用带有BjyAuthorize模块的 Zend Framework 2.4 并尝试为 ACL 规则创建一个断言类。问题是我需要对数据库进行查询以决定用户的权限(我正在使用 Doctrine ORM)。我试图将 ObjectManager 传递给 Assertion,但收到此错误:
Fatal error: Uncaught exception 'PDOException' with message 'You cannot serialize or unserialize PDO instances' in /var/www/vendor/zendframework/zendframework/library/Zend/Serializer/Adapter/PhpSerialize.php:48 Stack trace: #0 [internal function]: PDO->__sleep() #1 /var/www/vendor/zendframework/zendframework/library/Zend/Serializer/Adapter/PhpSerialize.php(48): serialize(Object(Zend\Permissions\Acl\Acl)) #2 /var/www/vendor/zendframework/zendframework/library/Zend/Cache/Storage/Plugin/Serializer.php(102): Zend\Serializer\Adapter\PhpSerialize->serialize(Object(Zend\Permissions\Acl\Acl)) #3 [internal function]: Zend\Cache\Storage\Plugin\Serializer->onWriteItemPre(Object(Zend\Cache\Storage\Event)) #4 /var/www/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(444): call_user_func(Array, Object(Zend\Cache\Storage\Event)) #5 /var/www/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(205): Zend\EventManager\EventManager->triggerListeners('setItem.pre', Object(Zend\Cache\Stor in /var/www/vendor/zendframework/zendframework/library/Zend/Serializer/Adapter/PhpSerialize.php on line 48
好的,就是它所说的。我的问题是:我该如何解决这个问题?
这是代码示例:
工厂:
<?php
namespace Foo\Permissions\Acl\Assertion;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Foo\Permissions\Acl\Assertion\IsUserDrunkAssertion;
class IsUserDrunkAssertionFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$em = $serviceLocator->get('doctrine.entitymanager.orm_default');
$assertion = new IsUserDrunkAssertion();
$assertion->setObjectManager($em);
return $assertion;
}
}
断言:
<?php
namespace Foo\Permissions\Acl\Assertion;
use Zend\Permissions\Acl\Assertion\AssertionInterface;
use Doctrine\Common\Persistence\ObjectManager;
// ...
class IsUserDrunkAssertion implements AssertionInterface
{
protected $objectManager;
public function setObjectManager(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
return $this;
}
public function getObjectManager()
{
return $this->objectManager;
}
public function assert(Acl $acl, RoleInterface $role = null, ResourceInterface $resource = null, $privilege = null)
{
$decision = // ... make a query
return $decision;
}
}
到目前为止,我只能提出两个想法:要么将 ServiceLocator 传递给 Assertion,要么在 Factory 中进行所有查询,然后将其结果作为聚合值传递。有没有更好的办法?