Symfony调用成员函数guessExtension()

时间:2017-09-04 13:19:51

标签: symfony

我尝试在我的实体上传文件,但实际上是

  

调用数组

上的成员函数guessExtension()

问题来自我控制器上的这一行:

  

$ 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" } }

2 个答案:

答案 0 :(得分:2)

所以我仍然不知道你在getFlyer()设置了你在哪里的值,但似乎你在这里有一个数组,只需做这样的事情:

$fileArray = $gig->getFlyer();
$file = $fileArray['file'];
$fileName = md5(uniqid()).'.'.$file->guessExtension();

答案 1 :(得分:0)

就我而言,我有一个像这样的setter和getter,这就是symfony实体构建器创建它的方式:

public function getHeader(): ?string {}
public function setHeader(?string $header): self {}

错误是因为you cast the result to string,删除了类型提示修复它:

public function getHeader(){}
public function setHeader($header): self {}