2013-05-30 102 views
0

我有一個PurchaseOrder實體,它與ArticleOrder實體具有OneToMany關係。此外,ArticleOrder實體不僅保存訂單和文章ID,還包含金額。此外,ArticleOrder實體與另一個名爲ArticleSupplier的實體具有OneToMany關係。這樣,每件商品的訂購數量就可以分成多個供應商。 現在我想構建一個表單來解決這個問題。所以,我創建了三個表單類型:多個嵌入式集合

其中一個僅保持ArticleOrders的嵌入式集合:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 

    $builder->add('purchaseOrders', 'collection', array('type' => new ProducedAmountOrderArticleType())); 
} 

public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'Acme\AppBundle\Entity\PurchaseOrder', 
    )); 
} 

其中一個持有ArticleSupplier條目的嵌入式集合:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('articleOrderReferences', 'collection', array('type' => new ProducedAmountOrderSubArticleType())); 

} 

public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'Acme\AppBundle\Entity\ArticleOrderReference', 
    )); 
} 

還有一對該文章供應商數量輸入字段:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('produced'); 

} 

public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'Acme\AppBundle\Entity\AOSupplierReference', 
    )); 
} 

小枝c頌:

<span class="purchaseOrders"> 
        {% for purchaseOrder in form.purchaseOrders %} 
         <span class="articleOrderReferences"> 
          {% for articleOrderReference in form.articleOrderReferences %} 
           {{ form_errors(articleOrderReference.produced) }} 
           {{ form_widget(articleOrderReference.produced, { 'attr': {'class': 'input-mini'} }) }} 
          {% endfor %}<br> 
         </span> 

        {% endfor %} 
       </span> 

控制器:

  $form = $this->createForm(new ProducedAmountOrderType(), $order); 

//   // process the form on POST 
//   if ($request->isMethod('POST')) { 
//   $form->bind($request); 
//   if ($form->isValid()) { 

//    $em = $this->getDoctrine()->getManager(); 
//    $em->persist($order); 
//    $em->flush(); 

//    return $this->redirect($this->generateUrl('order', array('id' => $orderId))); 
//   } 
//   } 

     return $this->render('AcmeAppBundle:ProducedAmount:index.html.twig', array(
       'form' => $form->createView(), 
       'order' => $order, 
       'articles' => $articles, 
     )); 

我評論的信息發佈出來,因爲這隻會導致一個空白頁。現在我總是得到這個錯誤信息:

Method "articleOrderReferences" for object "Symfony\Component\Form\FormView" does not exist in AcmeAppBundle:ProducedAmount:index.html.twig at line 150 

我做錯了什麼?

回答

2

此:

{% for articleOrderReference in form.articleOrderReferences %} 

應該是:

{% for articleOrderReference in **purchaseOrder**.articleOrderReferences %} 

降*的課程。我試圖讓它大膽。

+0

工作正常!非常感謝你! – ChrisS

+0

而當我只需要特定的輸入字段(如下所示:where supplier_id = $ id)是否有集合的查詢生成器選項? – ChrisS

+1

否。集合沒有任何種類的查詢生成器選項。但是,您可以在檢索原始$訂單並限制供應商時創建自定義查詢。不過,不同的問題。 – Cerad