dpl74687 2018-06-12 17:01
浏览 123

在null上调用成员函数isCollection()

I, using the Graphaware Neo4j-php-OGM. I would like to access the 2nd level relationship. I can't seen to get it to work. What am i doing wrong?

I'm trying to execute the following:

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;
    }

Stacktrace:

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)

It throws the error on the 2nd foreach loop on line:

foreach ($role->getPermissions() as $perm)

It's strange, working the first time correctly and not the 2nd time. Also after fetching the object again to be sure. Without this it throws the exact same notice. Thanks in advance!

All code is at github: https://github.com/djkevino/Support4Neo

  • 写回答

2条回答 默认 最新

  • dongque1646 2018-06-27 19:37
    关注

    Honestly, pretty confused by this code. The var_dump() die() stuff in destroy()... Also, took me a while to realize we are talking about code in the library, not your code that interacts with this library.

    Can you avoid the issue like so?

    //add this next line
    if (!is_iterable($user->getPermrole())) continue;
    foreach ($role->getPermissions() as $perm)
    

    If you are not on PHP version 7.1 or greater you could simply check if the returned value is_null or test for instance of \Traversable and is_array...

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

    As stated in my comment, not sure if that role having no "permissions" is considered a valid state or not.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀