1

I have one entity named Order, and other named OrderItens.

In my Order form, I am embedding OrderItens form, and on OrderItens form I am using an EntityType to retrieve Product Objects (that have price, name and other fields).

Right now I am able to submit the form and persist both entities, saving the Order PK as OrderItens FK, as expected. Also I can insert multiple products (I followed the documentation from symfony on embed forms) in the OrderType, creating two or more rows on the same form submit.

But I am having trouble retrieving the price of the Products selected and setting it for each specific Product.

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']);
}

OrderItensType.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()]
    )
    );
}

My problem is inside the foreach. I am having trouble retrieving and setting the Price for each product, specifically with the line $product = $form->get('orderItem')->getData()->getProduct()->getPrice();, which returns the error "Attempted to call an undefined method named "getProduct" of class "Doctrine\Common\Collections\ArrayCollection"".

If I explicitly number the array key e.g. like this $product = $form->get('orderItem')->getData()['1']->getProduct()->getPrice() the form submits, but the prices for all the products in the OrderItens will obviously be written as the same price in which belongs to Product['1'].

Any help is appreciated.

2
  • Create listener and set price there. Commented Jul 31, 2018 at 17:28
  • 1
    @kallosz yuck... Commented Jul 31, 2018 at 17:31

1 Answer 1

1

change

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

to

foreach ($form->get('orderItem')->getData() as $items){
        $product = $items->getProduct()->getPrice();
        $itens->setOrder($order);
        $itens->setPrice($product);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.