2017-01-02 40 views
1

我想存儲一些數據嵌套CollectionType保存實體的ArrayCollection

class Offer 
{ 
    /** 
    * @ORM\Column(type="array") 
    */ 
    private $meterPoints; 


    public function __construct() 
    { 
     $this->meterPoints = new ArrayCollection(); 
    } 
} 

的報價爲CollectionType。

class OfferType extends AbstractType 
{ 
    $builder 
     ->add('meterPoints', CollectionType::class, array(
      'entry_type' => OfferMeterPointType::class, 
      'allow_add' => true, 
      'allow_delete' => true, 
      'prototype' => true, 
      'prototype_name' => '__mp_name__', 
     )) 
} 

在OfferMeterPointType我也有一個的EntityType

class OfferMeterPointType extends AbstractType 
{ 
    $builder 
     ->add('meterPoint', EntityType::class, array(
      'attr' => array(
       'class' => 'chosen-select' 
      ), 
      'class' => 'AppBundle:MeterPoint', 
      'choice_label' => 'meterPoint', 
     )) 
     ->add('billData', CollectionType::class, array(
      'label' => false, 
      'entry_type' => OfferBillType::class, 
      'allow_add' => true, 
      'allow_delete' => true, 
      'entry_options' => array(
       'label' => false 
      ) 
     )); 

} 

現在,當我堅持認爲實體整個的appbundle:MeterPoint對象獲取序列化,而不僅僅是ID。我有點理解爲什麼教義這樣做,但我可以改變它,只有ID將被存儲?

此外,當我想編輯報價

$offer = $em->getRepository('AppBundle:Offer')->findOneById(2); 
$form = $this->createForm(OfferType::class, $offer); 

我得到一個異常

實體傳遞到選擇現場必須加以管理。也許堅持他們在實體經理?

我想一個解決方案是爲OfferMeterPointType創建一個實體,但我真的不想這樣做。因爲我幾乎從不需要這些數據。

更新

我試着像馬丁建議。現在異常走了,但它仍然保存完整的對象

$meterPoints = $this->em->getRepository('AppBundle:MeterPoint')->findAll(); 
    //dump($meterPoints); 
    $builder 
     ->add('meterPoint', ChoiceType::class, array(
      'label' => 'offerMeterPoint.meterPoint', 
      'attr' => array(
       'class' => 'chosen-selesct', 
       'placeholder' => 'ob.to' 
      ), 
      'choices' => $meterPoints, 
      'choice_label' => function($meterPoint) { 
       return $meterPoint->getMeterPoint(); 
      }, 
      'choice_value' => function($meterPoint) { 
       if($meterPoint === null){ 
        return null; 
       } 
       dump($meterPoint); 
       return $meterPoint->getId(); 
      }, 
      'placeholder' => 'global.plz_select' 
     )) 

更新2 得到它的工作 改變了ChoiceType

 $meterPoints = $this->em->getRepository('AppBundle:MeterPoint')->findAll(); 
    $mps = array(); 
    foreach($meterPoints as $mp){ 
     $mps [$mp->getMeterPoint()] = $mp->getId(); 
    } 
    //dump($meterPoints); 
    $builder 
     ->add('meterPoint', ChoiceType::class, array(
      'label' => 'offerMeterPoint.meterPoint', 
      'attr' => array(
       'class' => 'chosen-selesct', 
       'placeholder' => 'ob.to' 
      ), 
      'choices' => $mps, 
      'placeholder' => 'global.plz_select' 
     )) 

回答

0
  1. 你得到一個對象序列化,因爲你的列類型是arrayEntityType自動用對象替換選項值。

    然而,choice_value也接受可調用,所以我會嘗試擺弄它,也許你可以得到它只返回id(也許強制字符串類型?)。

    如果這沒有幫助,那麼可能只使用ChoiceType並自己處理邏輯。

  2. 發生這種情況是因爲Doctrine會自動反序列化您嘗試用作EntityType實體的$meterPoints中的對象。這些對象顯然不是由Doctrine Entity manager管理的。因此錯誤。

    我想你必須將$meterPoints轉換爲數據庫實體,然後才使用$this->createForm(...),如果你想避免創建更多的關係。最後,甚至更簡單的方法將編寫定製主義類型,可以爲你做這個: