2015-11-03 40 views
1
$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;每個變量後,我得到的結果,直到年底,只有當我刪除它,它給我的錯誤。什麼是解決方案?調用一個成員函數上的空ZF2學說ORM 2

+0

也許,你多次執行這個命令,'exit()'會在錯誤發生之前停止腳本。在'$ class = $ objectManager-> find [...]'後添加一個'if($ class === null){/ *在這裏添加調試信息*/die();}'。 –

+0

它崩潰了,但如果我做print_r($ class); exit;相反,我得到一個正常的結果 – f0unix

回答

0

嘗試用類似的東西進行調試:

$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(); 

就像我在我的評論說,也許代碼運行過程中出現多次。

+0

我編輯了我的迴應更多的調試信息。有了這個,你可以得到造成麻煩的對象的ID並解決它;) –

相關問題