Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to a member function isCollection() on null

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

like image 430
djkevino Avatar asked Jun 12 '18 17:06

djkevino


2 Answers

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.

like image 180
ficuscr Avatar answered Nov 07 '22 16:11

ficuscr


  1. It looks like $role->getPermissions() can return a null result. So, make sure the result is not null before executing the inner foreach loop.

  2. Since you seem to be testing if any of the user's roles has the desired permission, you do not need the $allowed variable at all. The inner foreach loop can just immediately return true once a match is found, avoiding unnecessary processing.

That is, try this:

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;
}
like image 21
cybersam Avatar answered Nov 07 '22 15:11

cybersam