对成员函数guessExtension()的Symfony调用
我试图在我的实体上上传一个文件,但实际上 对数组上的成员函数guessExtension()的调用 问题来自我的控制器上的这一行:对成员函数guessExtension()的Symfony调用,symfony,Symfony,我试图在我的实体上上传一个文件,但实际上 对数组上的成员函数guessExtension()的调用 问题来自我的控制器上的这一行: public function addGigAction(Request $request , $id ){ $em = $this->getDoctrine()->getManager(); $artist = $em->getRepository('BookingRoosterBundle:artist')-
public function addGigAction(Request $request , $id ){
$em = $this->getDoctrine()->getManager();
$artist = $em->getRepository('BookingRoosterBundle:artist')->find($id);
if (null === $artist) {
throw new NotFoundHttpException("L'annonce d'id ".$id." n'existe pas.");
}
$gig = new Gig();
//on inject l'artist directement
$gig->setArtist($artist);
$form = $this->createForm(GigType::class, $gig);
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
$file = $gig->getFlyer();
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$file->move(
$this->getParameter('upload_directory'),
$fileName
);
$gig->setFlyer($fileName);
$em->persist($gig);
$em->flush();
$request->getSession()->getFlashBag()->add('notice', 'Date bien ajouté pour l\'artiste');
return $this->redirectToRoute('booking_rooster_view', array(
'id' => $artist->getId()
));
}
return $this->render('BookingRoosterBundle:Artist:addGig.html.twig' , array(
'artist' => $artist,
'form' => $form->createView(),
));
}
$fileName=md5(uniqid())。$file->guessExtension()
有人知道我为什么会犯这个错误吗?希望有人能帮我解决这个问题。提前多谢
致意
这里是我的实体:
class Gig
{
private $flyer;
public function setFlyer($flyer)
{
$this->flyer = $flyer;
return $this;
}
public function getFlyer()
{
return $this->flyer;
}
...
这里是我的控制器:
public function addGigAction(Request $request , $id ){
$em = $this->getDoctrine()->getManager();
$artist = $em->getRepository('BookingRoosterBundle:artist')->find($id);
if (null === $artist) {
throw new NotFoundHttpException("L'annonce d'id ".$id." n'existe pas.");
}
$gig = new Gig();
//on inject l'artist directement
$gig->setArtist($artist);
$form = $this->createForm(GigType::class, $gig);
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
$file = $gig->getFlyer();
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$file->move(
$this->getParameter('upload_directory'),
$fileName
);
$gig->setFlyer($fileName);
$em->persist($gig);
$em->flush();
$request->getSession()->getFlashBag()->add('notice', 'Date bien ajouté pour l\'artiste');
return $this->redirectToRoute('booking_rooster_view', array(
'id' => $artist->getId()
));
}
return $this->render('BookingRoosterBundle:Artist:addGig.html.twig' , array(
'artist' => $artist,
'form' => $form->createView(),
));
}
这里是我从$file的转储:
array(1) { ["file"]=> object(Symfony\Component\HttpFoundation\File\UploadedFile)#37 (7) { ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false) ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(38) "techno_flyer_by_curtismack-d4lbtu5.png" ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(9) "image/png" ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(243010) ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0) ["pathName":"SplFileInfo":private]=> string(45) "/Applications/XAMPP/xamppfiles/temp/phpdfJ98s" ["fileName":"SplFileInfo":private]=> string(9) "phpdfJ98s" } }
所以我仍然不知道在哪里设置在
getFlyer()
中获得的值,但这里似乎有一个数组,只需执行以下操作:
$fileArray = $gig->getFlyer();
$file = $fileArray['file'];
$fileName = md5(uniqid()).'.'.$file->guessExtension();
在我的例子中,我有这样一个setter和getter,symfony实体生成器就是这样创建它的:
public function getHeader(): ?string {}
public function setHeader(?string $header): self {}
错误是因为,删除类型提示修复了它:
public function getHeader(){}
public function setHeader($header): self {}
您何时使用
$gig->getFlyer()
?你把你的$file转储了吗?是的,我转储了!我用转储文件更新帖子result@MathieuMourareau解决这个问题的真正原因是值得的。您的问题是,当您调用setFlyer()
时,您传递的是一个数组。在设置传单之前尝试修复它。我如何才能做到这一点?