I have a symfony web site and I'm doing its api rest, and I'm about to upload an image for an entity and I'm doing the postman tests, but when I realize it the JSON response turns me back to null. Note that I use vichuploaderbundle to upload my image.
Controller
/**
* @Route("/api/admin/assurance/logo/{id}")
* @Rest\View()
* @Method("PUT")
*/
public function putLogoEditAction(
Request $request,
Assurance $assurance,
$formType = null,
$logoId = null
){
$em = $this->getDoctrine()->getManager();
$logofile = $request->files->get('file');
$logofile = $this->getLogoFile($logoId);
$assurance->setLogo($logofile);
$form = $this->createForm('Doctix\MedecinBundle\Form\LogoType', $logofile);
$form->submit($request->request->all());
if ($form->isValid()){
$em = $this->getDoctrine()->getManager();
$em->persist($logofile);
$em->flush();
return $logofile;
}
return $form;
}
private function getLogoFile($logoId)
{
if (null === $logoId) {
return new Logo();
}
$logofile = $this->getDoctrine()->getRepository('DoctixMedecinBundle:Logo')->find($logoId);
return $logofile;
}
Entity
/*
* @Vich\UploadableField(mapping="media_image", fileNameProperty="logoName")
*
* @var File
*/
private $logoFile;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @var string
*/
private $logoName;
Form
->add('logoFile', VichImageType::class, [
'required' => false,
'allow_delete' => true,
'download_label' => true,
'download_uri' => true,
'image_uri' => true,
'imagine_pattern' => 'null',
]);
Test with Postman
I try to add a logo to an insurance, through the insurance id, and for that I add the next json, but it returns me a null with a code 200. This is a screenshot of my postman:
Get with Postman
Here you will find on this screenshot the result of the GET request that summarizes the contents of the insurance entity that has a name and a logo, and to which a doctor can affiliate
Thanks