代码之家  ›  专栏  ›  技术社区  ›  Joao Victor Souza

使用嵌入表单从数组中获取数据-Symfony 4

  •  1
  • Joao Victor Souza  · 技术社区  · 6 年前

    我有一个名为Order的实体,另一个名为OrderItens。

    在Order表单中,我嵌入了OrderItens表单,在OrderItens表单中,我使用EntityType检索产品对象(具有price、name和其他字段)。

    现在我可以提交表单并持久化这两个实体,如预期的那样将Order PK保存为OrderItens FK。 此外,我还可以在OrderType中插入多个产品(我遵循了symfony关于嵌入表单的文档),在同一个表单submit上创建两行或更多行。

    但我在检索所选产品的价格并为每个特定产品设置价格时遇到问题。

    OrderType.php文件

    public function buildForm(FormBuilderInterface $builder, array $options){
    
        $builder->add('date', TextType::class, ['label' => 'Date:'])
            ->add('orderItem', CollectionType::class, array(
                    'entry_type' => OrdenItensType::class,
                    'entry_options' => array('label' => false),
                    'allow_add' => true,
                    'prototype' => true,
                    'label' => false
                ))
    
        ->add('submit', SubmitType::class, ['label' => 'Submit']);
    }
    

    orderitentype.php

    public function buildForm(FormBuilderInterface $builder, array $options){
    
        $builder->add('product', EntityType::class, [
                'class' => Product::class,
                'label' => 'Product:'
            ]);
    }
    

    ProductController.php产品控制器

    public function pedidos(Request $request){
    
        $order = new Order();
    
        $form = $this->formFactory->create(OrderType::class, $order);
        $form->handleRequest($request);
    
        if ($form->isSubmitted() && $form->isValid()){
    
            foreach ($order->getOrderItem() as $itens){
                $product = $form->get('orderItem')->getData()->getProduct()->getPrice();
                $itens->setOrder($order);
                $itens->setPrice($product);
            }
    
            $this->entityManager->persist($order);
            $this->entityManager->flush();
    
            $this->flashBag->add('success', 'Order added!');
    
            return new RedirectResponse($this->router->generate('lancamento_pedidos_index'));
        }
    
        return new Response(
            $this->twig->render(
                'produto/pedido.html.twig',
                ['form' => $form->createView()]
        )
        );
    }
    

    我的问题出在前厅里面。 我在检索和设置每种产品的价格时遇到问题,特别是在生产线方面 $product = $form->get('orderItem')->getData()->getProduct()->getPrice(); ,返回错误“试图调用名为“Doctrine\Common\Collections\ArrayCollection”类的未定义方法“getProduct”。

    如果我显式地给数组键编号,例如 $product = $form->get('orderItem')->getData()['1']->getProduct()->getPrice() 表单提交了,但是订单中所有产品的价格显然都将写为属于产品[1]的相同价格。

    如有任何帮助,我们将不胜感激。

    1 回复  |  直到 6 年前
        1
  •  1
  •   kallosz    6 年前

    改变

        foreach ($order->getOrderItem() as $itens){
            $product = $form->get('orderItem')->getData()->getProduct()->getPrice();
            $itens->setOrder($order);
            $itens->setPrice($product);
        }
    

    foreach ($form->get('orderItem')->getData() as $items){
            $product = $items->getProduct()->getPrice();
            $itens->setOrder($order);
            $itens->setPrice($product);
        }