How to process following data structure through Symfony Forms?
An entity which holds collection of an entity which has a relation to itself:
Order
/** @var OrderProduct */
$orderProducts
public function getQuoteProducts()
{
return $this->quoteProducts;
}
public function addOrderProduct(OrderProduct $orderProduct)
{
if (!$this->orderProducts->contains($orderProduct)) {
$this->orderProducts[] = $orderProduct;
$orderProduct->setOrder($this);
}
return $this;
}
public function removeOrderProduct(OrderProduct $orderProduct)
{
if ($this->orderProducts->contains($orderProduct)) {
$this->orderProducts->removeElement($orderProduct);
}
return $this;
}
OrderProduct
/** @var Order */
$order
/** @var OrderProduct */
$relatedOrderProducts
public function addRelatedOrderProduct(OrderProduct $orderProduct)
{
if (!$this->relatedOrderProducts->contains($orderProduct)) {
$this->relatedOrderProducts[] = $orderProduct;
$orderProduct->setMainOrderProduct($this);
}
return $this;
}
public function removeRelatedOrderProduct(OrderProduct $orderProduct)
{
if ($this->relatedOrderProducts->contains($orderProduct)) {
$this->relatedOrderProducts->removeElement($orderProduct);
$orderProduct->setMainOrderProduct(null);
}
return $this;
}
The request:
'order' => [
'some_order_property' => 'value',
// ...
'orderProducts' => [
'some_order_product_property' => 'value',
// ...
'relatedOrderProducts' => [
[
'some_order_product_property' => 'value',
// ...
],
[
'some_order_product_property' => 'value',
// ...
],
]
]
]
Forms:
OrderType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('orderProducts', OrderProductCollectionType::class)
->add(...);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['data_class' => Order::class]);
}
OrderProductCollectionType
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'type' => OrderProductType::NAME,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true,
]);
}
OrderProductType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('relatedOrderProducts', RelatedOrderProductCollectionType::class)
->add(...);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['data_class' => OrderProduct::class]);
}
RelatedOrderProductCollectionType
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'type' => RelatedOrderProductType::NAME,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true,
]);
}
RelatedOrderProductType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(...);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['data_class' => OrderProduct::class]);
}
This setup works, only it doesn't relate OrderProducts
from RelatedOrderProductType
form to the Order
(so they have order_id
set to null
).
When this relation is forced (eg. with a FormEvent
listener), then all the relations are getting deleted on subsequent form submission. That's most likely due to orphanRemoval=true
set on the entity.
Probably some details are missing from my example, but me general question is: can this data structure be processed with Symfony Forms? If so, then how?
orphanRemoval
as you highligthed correclty in your question. – DonCallisto