我正在構建一個CRM系統,我遇到了一個我無法解決的奇怪問題。當我嘗試創建一個新的CustomerCase實體,我想分配CaseState實體,所以這是我在CustomerCaseController.php的createAction做:Symfony2傳遞參數爲空
$caseStateId = $request->request->get('caseState_id');
$caseState = $this->getDoctrine()->getManager()->getRepository('HSWAshaBundle:CaseState')->findOneById($caseStateId);
$entity = new CustomerCase();
$entity->setCaseState($caseState);
...... etc.....
一切工作只是罰款,直至setCaseState方法。運行setCaseState後,我得到以下錯誤:
Catchable Fatal Error: Argument 1 passed to HSW\AshaBundle\Entity\CustomerCase::setCaseState() must be an instance of HSW\AshaBundle\Entity\CaseState, null given, called in /home/webuser/Symfony/vendor/symfony/symfony/src/Symfony/Component/Form/Util/PropertyPath.php on line 538 and defined in /home/webuser/Symfony/src/HSW/AshaBundle/Entity/CustomerCase.php line 843
怪異的是,$ caseState真的是一個CaseState對象(例如因爲$ caseState->的getName()的作品,並給了我正確的名稱選定CaseState)。 對於一些令人興奮的原因,當我使用setCaseState方法時,它只會變爲空。如果我做$ entity-> setCaseState($ customerStateObject-> getId()),我得到相同的錯誤信息,但是這次null更改爲整數。
CustomerCase與CaseState具有manyToOne關係。
這很好,如果我使用formBuilder的add()方法並跳過所有這些手動工作,但由於我使用了一個非常具體的自動填充jQuery下拉框從嵌套樹結構中選擇CaseState,手動添加下拉菜單並使用$ request-> request-> get()讀取它。
我已經與這部分戰鬥了近三天了,並將不勝感激每一次我可以得到的幫助!
'$ caseStateId'是否有適當的值?你嘗試將它轉換爲(int)嗎? – topaz1008