2013-06-26 26 views
3

當我嘗試從事件PRE_SET_DATA中獲取數據時,我得到了具有良好價值的對象,但無法使用它。

這是我的測試代碼:

 $builder->addEventListener(
     FormEvents::PRE_SET_DATA, 
     function(FormEvent $event) use ($factory){ 

      $data = $event->getData(); 
      print_r($data); 

     } 
    ); 

這將返回長文本:

"YOU\CommercantBundle\Entity\LivraisonChoix Object ([id:YOU\CommercantBundle\Entity\LivraisonChoix:private] => 22 ..."

但是當我使用一個getter:

 $builder->addEventListener(
     FormEvents::PRE_SET_DATA, 
     function(FormEvent $event) use ($factory){ 

      $data = $event->getData(); 
      print_r($data->getId()); 

     } 
    ); 

我得到一個錯誤:

FatalErrorException: Error: Call to a member function getId() on a non-object

如何訪問數據?

這項工作適合PRE_BIND事件。

回答

6

我需要使用這個條件,吸氣工作:

 if ($data instanceof \YOU\CommercantBundle\Entity\LivraisonChoix) { 

     } 
相關問題