I can create a form which works fine but getting errors when trying to flush data to the database:
The controller action looks like this:
$purchase = new Purchase();
$form = $this->createFormBuilder($purchase)
->add('addresses', AddressType::class)
->add('quantity', IntegerType::class)
->add('toBeDeliveredAt', DateType::class)
->add('comment', TextareaType::class)
->add('save', SubmitType::class, array('label' => 'Submit'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($purchase);
$em->flush();
}
Class AddressType looks like this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class)
->add('firstname', TextType::class)
->add('lastname', TextType::class)
->add('street', TextType::class)
->add('city', TextType::class)
->add('zipcode', TextType::class)
->add('country', TextType::class)
->add('phone', TextType::class)
->add('email', EmailType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'XxxBundle\Entity\Address'
));
}
But that gives me the following error:
The form's view data is expected to be an instance of class XxxBundle\Entity\Address, but is an instance of class Doctrine\Common\Collections\ArrayCollection. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class Doctrine\Common\Collections\ArrayCollection to an instance of XxxBundle\Entity\Address.
If I set the data_class
option to null I get
Warning: spl_object_hash() expects parameter 1 to be object, string given
How can I add a view transformer that transforms an instance of class ArrayCollection to an instance of Address?