Reputation: 6830
I'm trying to create a form where I can place an order. My database schema looks like this:
I would like to have a form like this:
This is what I have so far:
$order = new Order();
$form = $this->createFormBuilder($order)
->add('naam', TextType::class)
->add('email', EmailType::class)
->add('phoneNumber', TextType::class)
->add('dateTakeout', DateType::class)
->add('hourTakeout', DateType::class)
->add('save', SubmitType::class, array('label' => 'Verzenden'))
->getForm();
But I'm stuck with adding the products to my form. How can I do this?
UPDATE:
The connection of my product and category is like this:
I have a Category entity with id, name, enabled and parentCategoryId.
In my Product entity I have a category property.
I would like to show the categories and products like this:
Upvotes: 1
Views: 1234
Reputation: 81
I see this as embedding multiple forms for the Entity OrderHasProducts
,
When initializing your form object, you will need to loop over all products (Or some of them depending on your requirements) and add them to the Order
new instance, then let symfony do its magic regarding creating the form.
To create the embedded form :
$builder->add('order_has_products', CollectionType::class, array(
'entry_type' => OrderHasProduct::class
));
And create your form for the OrderHasProduct entity
(check the docs How to Embed a Collection of Forms )
and in your controller , something like the following :
$order = new Order();
foreach($products as $product) :
// you may wanna create your custom method here
$order->addOrderProduct((new OrderHasProduct())
->setOrder($order)
->setProduct($product));
endforeach;
Upvotes: 0
Reputation: 7764
Add this to your form:
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
...
->add('product_A', IntegerType::class)
->add('product_B', IntegerType::class)
->add('product_C', IntegerType::class)
->add('product_D', IntegerType::class)
This is an Integer, so you can simply call:
$prodA_amount = $form->get('product_A')->getData();
to get the value...
Upvotes: 1