gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 12:39:41 26 4
gpt4 key购买 nike

我,使用 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)

它在第 2 个 foreach 循环上抛出错误:

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

很奇怪,第一次正常工作,第二次却不行。还要再次获取对象后才能确定。没有这个它会抛出完全相同的通知。提前致谢!

所有代码都在github: https://github.com/djkevino/Support4Neo

最佳答案

老实说,这段代码让我很困惑。 var_dump() die() 中的东西 destroy()...另外,我花了一段时间才意识到我们在谈论库中的代码,而不是你的代码与这个库交互。

你能避免这样的问题吗?

//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 ));

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

关于php - 在 null 上调用成员函数 isCollection(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50822253/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com