Zend:在断言中使用EntityManager

时间:2015-05-12 18:12:36

标签: php zend-framework doctrine-orm bjyauthorize

我正在使用带有BjyAuthorize模块的Zend Framework 2.4并尝试为ACL规则创建一个Assertion类。问题是我需要对数据库进行查询以决定用户的权限(我使用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中进行所有查询,然后将其结果作为聚合值传递。有没有更好的办法?

0 个答案:

没有答案