I get an error when trying to store a form with multiple file input.
I have two entities: Inquiry and UploadedDocumend which are in a one-to-many relation.
Here is the transformer for the uploaded files:
public function reverseTransform($uploadedDocuments)
{
$attachments = array();
foreach($uploadedDocuments as $file){
if($file) {
$uploadedDocument = new UploadedDocument();
/* @var $uploadedDocument UploadedDocument */
$uploadedDocument->setFile($file);
$uploadedDocument->setCreatedOn(new \DateTime("now"));
$this->em->persist($uploadedDocument);
$attachments[] = $uploadedDocument;
}
}
return $attachments;
}
And here the code to add the transformer:
->add($builder->create(
'uploadedDocuments', 'file', [
'required' => false,
'multiple' => true,
'label' => "Anhänge"
]
)->addModelTransformer(new UploadsTransformer($this->em)))
With this I get the following exception:
The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, 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 Symfony\Component\HttpFoundation\File\File.
I can't change the resolver. Because I need it that way. I think.
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'Lengoo\FormhandlerBundle\Entity\Inquiry',
));
}
How do I have to change the transformer so the object is created correctly? FYI: It works if I just store the path from the file to an array. But I want the relation between the two objects.