我有一个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',
));
}
还有一个用于ArticleSupplier Amount输入字段:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('produced');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\AppBundle\Entity\AOSupplierReference',
));
}
树枝代码:
<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
我做错了什么?
答案 0 :(得分:2)
此:
{% for articleOrderReference in form.articleOrderReferences %}
应该是:
{% for articleOrderReference in **purchaseOrder**.articleOrderReferences %}
放弃*当然。我试着大胆一点。