在字符串上调用成员函数guessExtension()

时间:2018-04-02 02:32:57

标签: symfony symfony4

在Symfony 4中我收到此错误: 在字符串上调用成员函数guessExtension() 在之前我使用相同的代码上传图像并做得很好,但在这里我得到了错误。 有没有人遇到同样的问题并解决了?

$em = $this->getDoctrine()->getManager();
    $imageEn = new Image();

     $form = $this->createForm(ImageUploadType::class, $imageEn);

     $form->handleRequest($request);

     if($form->isSubmitted() && $form->isValid()){


     /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */

        $file = $imageEn->getImage();

        $fileName = md5(uniqid()).'.'.$file->guessExtension();

        $file->move($this->getParameter('image_directory'), $fileName);

        $imageEn->setImage($fileName);
        $em->persist($imageEn);
        $em->flush();

        $this->addFlash('notice', 'Post Submitted Successfully!!!');

        return $this->redirectToRoute('image_upload');
     }

形式:

  {
    $builder
        ->add('title', TextType::class)
        ->add('description', TextType::class)
        ->add('image', FileType::class, array('label'=>'Upload Image'))
        ->add('submit', SubmitType::class)
    ;
}

图片类:

实际上我按照他手动创建这个类的教程但是我使用Command来创建类,我用他的所有代码计算它是正确的。

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ORM\Entity(repositoryClass="App\Repository\ImageRepository")
*/
class Image
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(type="string", length=255)
 */
private $title;

/**
 * @ORM\Column(type="string", length=255)
 */
private $description;

/**
 * @ORM\Column(type="string", length=255)
 * @Assert\NotBlank(message="Please upload image")
 * @Assert\File(mimeTypes={"image/jpeg"})
 */
private $image;

public function getId()
{
    return $this->id;
}

public function getTitle(): ?string
{
    return $this->title;
}

public function setTitle(string $title): self
{
    $this->title = $title;

    return $this;
}

public function getDescription(): ?string
{
    return $this->description;
}

public function setDescription(string $description): self
{
    $this->description = $description;

    return $this;
}

public function getImage(): ?string
{
    return $this->image;
}

public function setImage(string $image): self
{
    $this->image = $image;

    return $this;
}

}

2 个答案:

答案 0 :(得分:7)

替换

$file = $imageEn->getImage()

$file = $form->get('image')->getData();

答案 1 :(得分:2)

对我来说,是实体的打字提示弄乱了事情:

var arr = [{range:[0,20],title:"First",data:["a","b"]},{range:[20,40],title:"Second",data:["d","f"]},{range:[40,60],title:"THird",data:["g","k"]}];

function getRange( value ){
    return arr.findIndex(v => value >= v.range[0] && value <= v.range[1]);
}

console.log(getRange(22));
console.log(getRange(50));