I'm trying to upload files with ajax calls, but when I go to save my saves only the relationship with anagrafic. I state that if I try to save normally work correctly.
It is as if it not loaded the object UploadFile! I created the table according to the symfony cookbook http://symfony.com/doc/2.1/cookbook/doctrine/file_uploads.html In my controller
public function fileCreateAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MyBusinessBundle:Anagrafic')->find($id);
$media = new Multimedia();
$form = $this->createForm(new MultimediaType(), $media);
if ($this->getRequest()->isMethod('POST')) {
$form->bind($this->getRequest());
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$media->setAnagrafic($entity);
$em->persist($media);
$em->flush();
$response = new Response();
$output = array('success' => true);
$response->headers->set('Content-Type', 'application/json');
$response->setContent(json_encode($output));
}
}
I tried to do a var_dump of var_dump($media); and return:
object(My\BusinessBundle\Entity\Multimedia)[337]
private 'id' => null
private 'percorso' => null
private 'alt' => null
private 'type' => null
public 'file' => null
private 'anagrafic' => null
I don't understand why.. but if I use a plug-in jquery https://github.com/blueimp/jQuery-File-Upload to pass the file, I get the correct file that I can continue with the method of the cookbook! my solution:
public function fileCreateAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MyBusinessBundle:Anagrafic')->find($id);
$media = new Multimedia();
$form = $this->createForm(new MultimediaType(), $media);
$request = $this->getRequest();
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$media->setAnagrafic($entity);
$em->persist($media);
$em->flush();
if ($request->isXmlHttpRequest()) {
$response = new Response();
$output = array('success' => true);
$response->headers->set('Content-Type', 'application/json');
$response->setContent(json_encode($output));
return $response;
} else {
return $this->redirect($this->generateUrl('user_img', array('id' => $entity->getId())));
}
} else {
if ($request->isXmlHttpRequest()) {
$errors = $form->get('file')->getErrors();
$response = new Response();
$output = array('success' => false, 'errors' => $errors[0]->getMessage());
$response->headers->set('Content-Type', 'application/json');
$response->setContent(json_encode($output));
return $response;
}
}
}
would be to refactor given the double call -> isXmlHttpRequest (), but the concept works! I forgot .. if you set the input file as "multiple" a request is made for each file!
Solved!