调用空 ZF2 原则 ORM 2 上的成员函数


Call to a member function on null ZF2 Doctrine ORM 2

$section =  $objectManager->find('OEC'Entity'Section', $sectionId );
$class   =  $objectManager->find('OEC'Entity'Classes', $section->getClassId() );
$cycle   =  $objectManager->find('OEC'Entity'Cycle', $class->getCycleId() );
$branch  =  $objectManager->find('OEC'Entity'Branch', $cycle->getBranchId() );
$sectionArr = $class->getClassName()." ".$section->getSectionName()." - ". $branch->getBranchName()." ".$cycle->getCycleName();
$objectManager->close();

我得到了Call to a member function getCycleId() on null,尽管如果我在每个变量之后print_r($variable);exit;,我会得到一个结果直到最后,只有当我删除它时,它才会给我错误。有什么解决办法?

尝试使用如下内容对其进行调试:

$section =  $objectManager->find('OEC'Entity'Section', $sectionId );
$class   =  $objectManager->find('OEC'Entity'Classes', $section->getClassId() );
if ($class === null) {
    /* add debug info here*/ 
    var_dump('ID of class was '.$section->getClassId());
    var_dump('ID of section was '.$sectionId);
    var_dump((new 'Exception())->getTraceAsString());
    die();
}
$cycle   =  $objectManager->find('OEC'Entity'Cycle', $class->getCycleId() );
$branch  =  $objectManager->find('OEC'Entity'Branch', $cycle->getBranchId() );
$sectionArr = $class->getClassName()." ".$section->getSectionName()." - ". $branch->getBranchName()." ".$cycle->getCycleName();
$objectManager->close();

就像我在评论中所说的那样,也许代码多次运行。