2012-01-11 12 views
3

私はProduct,ProductFeatures,Goods,GoodsFeaturesValueという4つのエンティティとそれらの間の関係を持っています。 FeaturesProductに追加してから、静的フィールドのフォームを作成します。Goods + some some FeaturesProductこれはGoodsです。 Goodsの値はGoodsFeaturesValueに保存されています。symfony 2フォームタイプコレクションモデルから新しいプロパティごとにラベルを変更

「symfony way」でこのフォームを作成するにはどうすればよいですか?

は、私が他のFeaturesのためのコレクションを使用して、この作品の罰金

を更新しましたが、私は、各値のProductFeatures関係からラベルを設定することができますか?私は寺院をレンダリングするときにこれを行うことができますが、これは悪い:)?

//GoodsFormType class 
public function buildForm(FormBuilder $builder, array $options) 
{ 
    $builder 
      ->add('name') 
      //other property... 
      ->add('values', 'collection', array(
       'required' => true, 
       'type' => new GoodsFeaturesValueFormType(), 
       'allow_add' => false, 
       'allow_delete' => false, 
       'by_reference' => false, 
      )) 
    ; 
} 

//GoodsFeaturesValueFormType 
    public function buildForm(FormBuilder $builder, array $options) 
{ 
    $builder 
      ->add('value', 'text') 
    ; 
} 
    //controller 
    public function saveAction($id) 
{ 
    $em = $this->getDoctrine()->getEntityManager(); 
    $product = $em->getRepository('ShopCoreBundle:Product')->find($id); 

    if (!$product) 
     throw $this->createNotFoundException(sprintf('Product with id %s not found', $id)); 

    $features = $em->getRepository('ShopCoreBundle:ProductFeatures')->findByProduct($id); 
    $goods = new Goods(); 
    $goods->setProduct($product); 

    foreach ($features as $feature) { 
     $entity = new GoodsFeaturesValue(); 
     $entity->setFeatures($feature); 
     $entity->setGoods($goods); 
     $entity->setProduct($product); 
     $goods->addGoodsFeaturesValue($entity); 
    } 

    $request = $this->getRequest(); 

    $form = $this->createForm(new GoodsFormType(), $goods); 
    $form->bindRequest($request); 

    if ($form->isValid()) { 
     $em->persist($goods); 
     $em->flush(); 
     return $this->redirect($this->generateUrl('core_product_index')); 
    } 



    return array(
     'form' => $form->createView(), 
     'goods' => $goods, 
     'product' => $product, 
     'features' => $features, 
    ); 
} 

答えて

0

あなたはcollection Field Type、簡単な例を使用しようとすることができます:http://symfony.com/doc/current/reference/forms/types/collection.html#adding-and-removing-items

+0

次のようにResizeFormListener(コレクションの使用を)変更が、どのように新しい製品を作るためにそれを使うのか? コレクションタイプは、1対多リレーションシップに使用されます。 – rtyshyk

+0

ありがとう、私は試しましたが、新しい問題が...私の質問を更新 – rtyshyk

3

これは私が動的属性のために望んでいたまさにです。 FormEventEventSubscriberを使用して、これを動的生成フォームとして行うことができます。 http://symfony.com/doc/master/cookbook/form/dynamic_form_generation.html

ので、GoodsFeaturesValueFormTypeクラスでは、新しいEventSubscriberを作成し、preSetDataで、データとラベルを設定します。

更新日:symfonyのデフォルトのResizeFormListenerは値を渡さないため、エラーになります。これをサポートするために、私は製品のための機能を追加するためにそれを使用

[before] 
    91   // Then add all rows again in the correct order                
    92   foreach ($data as $name => $value) {                  
    93    $form->add($this->factory->createNamed($this->type, $name, null, array_replace(array(     
    94     'property_path' => '['.$name.']',                 
    95   ), $this->options))); 
    96   } 

[modified] 
    91   // Then add all rows again in the correct order                
    92   foreach ($data as $name => $value) {                  
    93    $form->add($this->factory->createNamed($this->type, $name, $value, array_replace(array(     
    94     'property_path' => '['.$name.']',                 
    95   ), $this->options))); 
    96   } 
関連する問題