2012-10-17 176 views
4

我有上訴實體; SiteTariff,用戶實體;關稅,現場實體 如何將驗證移動到實體。驗證複雜:包含相關實體。我試圖寫一個回調(在實體)=>沒有結果。Symfony 2將驗證移至實體,與相關實體驗證

$appeal = new Appeal(); 
    $form = $this->createFormBuilder($appeal); 

    if ($request->isMethod('POST')) 
    { 
     $form->bind($request); 

     $appeal->setUser($user); //addRelation 

     $appeal->appeals_count = $value; //set value to public field 

     if ($site_tariff_id = $form->getData()->site_tariff_id) 
     { 
      $siteTariff = $em->find('', $site_tariff_id); 
      $appeal->setSiteTariff($siteTariff); //addRelation 
     } 
     else 
     { 
      //addError, break 
     }      


     if ($appeal->getSiteTariff()->getSite()->getUser()->getId() != $user->getId()) 
     { 
      //addError 
     } 

     if ($appeal->getSiteTariff()->getTariff()->getAppealsNumber() <= $appeal->appeals_count) 
     { 
      //addError 
     } 

     if ($form->isValid()) 
     { 
      $em->persist($appeal); 
     } 
    } 

在回調,我沒有得到$ siteTariff對象調用$this->getSiteTariff()後,symfony中寫道:「非對象

public function isSiteTariffValid(ExecutionContext $context) 
    { 
     var_dump($this->getSiteTariff()->getId());  
    } 
+0

我不是symfony專家,但行是「$ siteTariff = $ em-> find('',$ site_tariff_id);」正確嗎? $ em是指您的實體經理或您的存儲庫? ($ em-> getRepository(entity) - > find($ id)) – SmasherHell

+0

什麼是當前類,它是父類?因爲我認爲'getSiteTariff()'是一個實體的方法,而不是驗證類。 – martinqt

回答