I have a form with multiple rows created from one table (no relationships to other tables). When I save the form, every change I've made is saved, but I do have an additional empty row in the database. See below for (hopefully) all neccessary informations.
PropertyAdditionCostFrequency.php
/**
* @ORM\Entity
* @ORM\Table(name="property_addition_cost_frequency")
*/
class PropertyAdditionCostFrequency
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $label = '';
private $costFrequencyCollection;
public function __construct()
{
$this->costFrequencyCollection = new ArrayCollection();
}
// + the getters and setters
}
PropertyAdditionCostFrequencyLabelType.php
class PropertyAdditionCostFrequencyLabelType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('label' )
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => PropertyAdditionCostFrequency::class,
));
}
}
PropertyAdditionCostFrequencyForm.php
class PropertyAdditionCostFrequencyForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add( 'cost_frequency_collection', CollectionType::class, array(
'entry_type' => PropertyAdditionCostFrequencyLabelType::class,
'label' => ''
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => PropertyAdditionCostFrequency::class
]);
}
}
AdminCoreDataController.php
public function showCoreDataListAction( Request $request )
{
$PropertyAdditionCostFrequency = new PropertyAdditionCostFrequency();
$repository = $this->getDoctrine()->getRepository('AppBundle:PropertyAdditionCostFrequency');
$cost_frequency = $repository->findAll();
foreach ($cost_frequency as $k => $v) {
$PropertyAdditionCostFrequency->getCostFrequencyCollection()->add($v);
}
$form = $this->createForm( PropertyAdditionCostFrequencyForm::class, $PropertyAdditionCostFrequency);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$propertyAdditionCostFrequency_form = $form->getData();
$em = $this->getDoctrine()->getManager();
$em->persist($propertyAdditionCostFrequency_form);
$em->flush();
$this->addFlash('success', 'successfully changed the data');
return $this->redirectToRoute('admin_core_data');
}
return $this->render('logged_in/content/admin/core_data/core_data.html.twig', [
'propertyCostFrequencyForm' => $form->createView()
]);
}
core_data.html.twig
{{ form_start(propertyCostFrequencyForm) }}
<div class="row" id="cost_frequency_box">
{% for single_frequency in propertyCostFrequencyForm.cost_frequency_collection %}
<div class="row js-single-cost-frequency-box">
<div class="col-sm-1 js-delete-cost-frequency">
<a href="/admin/property/delete/5">
<i class="fa fa-trash"></i>
</a>
</div>
<div class="col-sm-4">
{{ form_widget(single_frequency.label) }}
</div>
</div>
{% endfor %}
</div>
<div class="row">
<div class="col-sm-3">
<button class="col-sm-12 btn btn-success" type="submit">Speichern</button>
</div>
</div>
{{ form_end(propertyCostFrequencyForm) }}
a dump() of $propertyAdditionCostFrequency_form right before '$em->persist($propertyAdditionCostFrequency_form)'
PropertyAdditionCostFrequency {#394 ▼
-id: null
-label: ""
-costFrequencyCollection: ArrayCollection {#395 ▼
-elements: array:4 [▼
0 => PropertyAdditionCostFrequency {#422 ▼
-id: 1
-label: "1"
-costFrequencyCollection: null
}
1 => PropertyAdditionCostFrequency {#424 ▼
-id: 2
-label: "2"
-costFrequencyCollection: null
}
2 => PropertyAdditionCostFrequency {#425 ▼
-id: 47
-label: "3"
-costFrequencyCollection: null
}
3 => PropertyAdditionCostFrequency {#426 ▼
-id: 38
-label: "4"
-costFrequencyCollection: null
}
]
}
}
The reason why this is happening is evident from the dump you have posted: the top level PropertyAdditionCostFrequency has the id set to null, so of course it is going to create a new row for that, so if you were simply wondering why it creates a new row thats your reason :).
On the other hand I can't help but think, you have misunderstood how symfony forms / doctrine works, UNLESS having multiple child PropertyAdditionCostFrequency added to one parent PropertyAdditionCostFrequency is the desired effect.
IF it is not the desired effect and you simply want to create a form which contains a form for every entry of PropertyAdditionCostFrequency in the database(which I would not recommend, because once the DB gets big, it won't be very efficient), you can just use createFormBuilder method inside the controller, add an arbitrary CollectionType field (i.e. $builder->add('frequencies', CollectionType::class), then just create an array and assign the result of findAll() to a key(key name should correspond to the field name, in this case, 'frequencies') and finally set that as the data for the form you have just created (use $builder->getForm() method to get the form, since thats what you want in the controller and you should use the set data method on that :)) (P.S. these are just rough guidelines, I'm not sure, that I haven't missed anything)