我正在尝试使用ajax调用上传文件,但是当我去保存我的保存只与anagrafic的关系。 我声明如果我试图保存正常工作。
好像它没有加载对象UploadFile! 我根据symfony cookbook http://symfony.com/doc/2.1/cookbook/doctrine/file_uploads.html创建了表格 在我的控制器中
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));
}
}
我试着做var_dump的var_dump($ media);并返回:
object(My\BusinessBundle\Entity\Multimedia)[337]
private 'id' => null
private 'percorso' => null
private 'alt' => null
private 'type' => null
public 'file' => null
private 'anagrafic' => null
答案 0 :(得分:0)
我不明白为什么.. 但是如果我使用插件jquery https://github.com/blueimp/jQuery-File-Upload传递文件,我会得到正确的文件,我可以继续使用cookbook的方法! 我的解决方案:
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;
}
}
}
给定双重调用将重构 - > isXmlHttpRequest(),但这个概念有效! 我忘了..如果你将输入文件设置为“多个”,则会对每个文件发出请求!
解决!