在空值上调用成员函数isCollection()

8

我使用Graphaware Neo4j-php-OGM。我想访问第二层关系,但似乎无法正常运行。我做错了什么?

我正在尝试执行以下操作:

public function allowToContinue($userUuid, $permissionUuid)
    {
        $userRepo = $this->entityManager->getRepository(User::class);
        $permRoleRepo = $this->entityManager->getRepository(PermRole::class);
        $permissionRepo = $this->entityManager->getRepository(Permission::class);
        $user = $userRepo->findOneBy(['uuid' => $userUuid]);
        $permission = $permissionRepo->findOneBy(['uuid' => $permissionUuid]);
        $allowed = false;
        foreach ($user->getPermrole() as $userRole)
        {
            var_dump($userRole);
            $role = $permRoleRepo->findOneBy(['uuid' => $userRole->getUuid()]);
            var_dump($role);
            foreach ($role->getPermissions() as $perm)
            {
                var_dump($perm->getUuid());
                if($perm->getUuid() === $permissionUuid){
                    $allowed = true;
                }
            }
        }
        return $allowed;
    }

堆栈跟踪:

Error:
Call to a member function isCollection() on null

  at vendor/graphaware/neo4j-php-ogm/src/Hydrator/EntityHydrator.php:107
  at GraphAware\Neo4j\OGM\Hydrator\EntityHydrator->hydrateSimpleRelationshipCollection('permissions', object(Result), object(neo4j_ogm_proxy_App_Entity_Generic_PermRole))
     (vendor/graphaware/neo4j-php-ogm/src/Persisters/BasicEntityPersister.php:104)
  at GraphAware\Neo4j\OGM\Persisters\BasicEntityPersister->getSimpleRelationshipCollection('permissions', object(neo4j_ogm_proxy_App_Entity_Generic_PermRole))
     (vendor/graphaware/neo4j-php-ogm/src/Proxy/NodeCollectionInitializer.php:22)
  at GraphAware\Neo4j\OGM\Proxy\NodeCollectionInitializer->initialize(object(Node), object(neo4j_ogm_proxy_App_Entity_Generic_PermRole))
     (vendor/graphaware/neo4j-php-ogm/src/Proxy/LazyCollection.php:52)
  at GraphAware\Neo4j\OGM\Proxy\LazyCollection->doInitialize()
     (vendor/doctrine/collections/lib/Doctrine/Common/Collections/AbstractLazyCollection.php:332)
  at Doctrine\Common\Collections\AbstractLazyCollection->initialize()
     (vendor/doctrine/collections/lib/Doctrine/Common/Collections/AbstractLazyCollection.php:274)
  at Doctrine\Common\Collections\AbstractLazyCollection->getIterator()
     (src/Security/RoleChecker.php:45)
  at App\Security\RoleChecker->allowToContinue('8d88d920-5ab0-11e8-a371-001c42dff143', 'd93370b0-585d-11e8-a371-001c42dff143')
     (src/Controller/Generic/UserController.php:146)
  at App\Controller\Generic\UserController->destroy('c34f1380-5ab5-11e8-a371-001c42dff143')
     (vendor/symfony/http-kernel/HttpKernel.php:149)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:66)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:188)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:37)

它在第二个foreach循环的第几行代码上抛出错误: foreach ($role->getPermissions() as $perm) 很奇怪,第一次正常工作而第二次不起作用。即使重新获取对象也是如此。没有它会抛出完全相同的通知。
谢谢!所有的代码都在github上:https://github.com/djkevino/Support4Neo

1
你的建议让我想到其中一个角色在权限方面没有被分配。这是有效的状态吗?错误很明显,无法对空值执行该方法。实际上,让我注意到的不是在 $allowed = true; 上中断循环。此时,你不想退出循环吗? - ficuscr
2个回答

1
  1. 看起来 $role->getPermissions() 可能会返回 null 结果。因此,在执行内部的 foreach 循环之前,请确保结果不是 null

  2. 由于您似乎正在测试用户的任何角色是否具有所需的权限,因此根本不需要 $allowed 变量。一旦找到匹配项,内部的 foreach 循环就可以立即返回 true ,避免不必要的处理。

也就是说,尝试这样做:

public function allowToContinue($userUuid, $permissionUuid) {
    $userRepo = $this->entityManager->getRepository(User::class);
    $permRoleRepo = $this->entityManager->getRepository(PermRole::class);
    $permissionRepo = $this->entityManager->getRepository(Permission::class);
    $user = $userRepo->findOneBy(['uuid' => $userUuid]);
    $permission = $permissionRepo->findOneBy(['uuid' => $permissionUuid]);        
    foreach ($user->getPermrole() as $userRole) {
        var_dump($userRole);
        $role = $permRoleRepo->findOneBy(['uuid' => $userRole->getUuid()]);
        var_dump($role);
        $rolePerms = $role->getPermissions();
        if (!is_null($rolePerms)) {
            foreach ($rolePerms as $perm) {
                var_dump($perm->getUuid());
                if ($perm->getUuid() === $permissionUuid) {
                    return true;
                }
            }
        }
    }
    return false;
}

1
老实说,这段代码让我很困惑。在destroy()中,使用了var_dump() die()相关的东西... 而且,花了我一段时间才意识到我们讨论的是库中的代码,而不是你与这个库交互的代码。
你能否通过以下方式避免这个问题?
//add this next line
if (!is_iterable($user->getPermrole())) continue;
foreach ($role->getPermissions() as $perm)

如果您的PHP版本低于7.1,您可以简单地检查返回值是否为is_null或测试instance of \Traversableis_array...

is_array($foo) || (is_object($foo) && ($foo instanceof \Traversable ));

正如我在评论中所述,不确定该角色是否没有“权限”被视为有效状态。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接