Symfony 5 VichUploaderBundler:类“App\Entity\xxx”中方法“setImageFile”中参数“imageFile”的类型提示无效

Posted

技术标签:

【中文标题】Symfony 5 VichUploaderBundler:类“App\\Entity\\xxx”中方法“setImageFile”中参数“imageFile”的类型提示无效【英文标题】:Symfony 5 VichUploaderBundler : The type hint of parameter "imageFile" in method "setImageFile" in class "App\Entity\xxx" is invalidSymfony 5 VichUploaderBundler:类“App\Entity\xxx”中方法“setImageFile”中参数“imageFile”的类型提示无效 【发布时间】:2021-11-11 05:35:29 【问题描述】:

在生产缓存清除期间,我收到此消息:“App\Entity\Evenement”类中方法“setImageFile”中参数“imageFile”的类型提示无效。我按照 github“基本用法”上的 VichUploaderBundle 文档的说明进行操作。 感谢您的帮助!

实体代码下方:

<?php

namespace App\Entity;

use App\Repository\EvenementRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\component\HttpFoundation\File\File;
use Symfony\component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity(repositoryClass=EvenementRepository::class)
 * @vich\Uploadable
 */
class Evenement

    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

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

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

    /**
     * @ORM\Column(type="string", length=500, nullable=true)
     * @Assert\Url()
     */
    private $visuel;

    /**
     * @Vich\UploadableField(mapping="evenement", fileNameProperty="imageName")
     * @var File|null
     */
    private $imageFile;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     * @var string|null
     */
    private $imageName;

    /**
     * @ORM\Column(type="datetime")
     *
     * @var \DateTimeInterface|null
     */
    private $updatedAt;

    /**
     * @ORM\Column(type="string", length=500, nullable=true)
     */
    private $tarifs;

    /**
     * @ORM\Column(type="datetime")
     */
    private $date;

    /**
     * @ORM\ManyToMany(targetEntity=Film::class, inversedBy="evenements")
     */
    private $films;

    /**
     * @ORM\Column(type="date", nullable=true)
     */
    private $dateFin;

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

    public function __construct()
    
        $this->films = new ArrayCollection();
    




    public function getId(): ?int
    
        return $this->id;
    

    public function getTitre(): ?string
    
        return $this->titre;
    

    public function setTitre(string $titre): self
    
        $this->titre = $titre;

        return $this;
    

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

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

        return $this;
    

    public function getVisuel(): ?string
    
        return $this->visuel;
    

    public function setVisuel(?string $visuel): self
    
        $this->visuel = $visuel;

        return $this;
    

    public function getTarifs(): ?string
    
        return $this->tarifs;
    

    public function setTarifs(?string $tarifs): self
    
        $this->tarifs = $tarifs;

        return $this;
    

    public function getDate(): ?\DateTimeInterface
    
        return $this->date;
    

    public function setDate(\DateTimeInterface $date): self
    
        $this->date = $date;

        return $this;
    

    /**
     * @return Collection|Film[]
     */
    public function getFilms(): Collection
    
        return $this->films;
    

    public function addFilm(Film $film): self
    
        if (!$this->films->contains($film)) 
            $this->films[] = $film;
        

        return $this;
    

    public function removeFilm(Film $film): self
    
        $this->films->removeElement($film);

        return $this;
    

    public function getDateFin(): ?\DateTimeInterface
    
        return $this->dateFin;
    

    public function setDateFin(?\DateTimeInterface $dateFin): self
    
        $this->dateFin = $dateFin;

        return $this;
    

    public function getVideo(): ?string
    
        return $this->video;
    

    public function setVideo(?string $video): self
    
        $this->video = $video;

        return $this;
    


    /**
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
     * of 'UploadedFile' is injected into this setter to trigger the update. If this
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
     * must be able to accept an instance of 'File' as the bundle will inject one here
     * during Doctrine hydration.
     *
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
     */
    public function setImageFile(?File $imageFile = null): void
    
        $this->imageFile = $imageFile;

        if (null !== $imageFile) 
            // It is required that at least one field changes if you are using doctrine
            // otherwise the event listeners won't be called and the file is lost
            $this->updatedAt = new \DateTimeImmutable();
        
    

    public function getImageFile(): ?File
    
        return $this->imageFile;
    

    public function setImageName(?string $imageName): void
    
        $this->imageName = $imageName;
    

    public function getImageName(): ?string
    
        return $this->imageName;
    

【问题讨论】:

你好 Olivv,请尝试清除 dev mod 的缓存 你好,Ravi,dev mod 的缓存清除在本地和服务器上工作,但 prod mod 的缓存清除在本地和服务器上不起作用。 您在产品服务器上尝试过rm -rf var/cache/* 吗? 它以这种方式工作。在拥有 "rm -rf var/cache/*" 之后,我还必须上传代理目录。但是现在,如何让“php bin/console cache:clear --env=prod”命令再次工作? rm -rf /var/cache/* 之后尝试php bin/console cache:clear --env=prod 命令 【参考方案1】:

请根据此设置您的字段

注意:别忘了更新你的数据库

use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Entity\File as EmbeddedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

  /**
 * NOTE: This is not a mapped field of entity metadata, just a simple property.
 *
 * @Assert\File(
 *     maxSize="51200k",
 *     mimeTypes = 
 *       "image/jpeg",
 *       "image/gif",
 *       "image/png",
 *     
 * )
 * @Vich\UploadableField(
 *      mapping="evenement_image",
 *      fileNameProperty="imageFile.name",
 *      size="imageFile.size",
 *      originalName="imageFile.originalName",
 *      mimeType="imageFile.mimeType"
 * )
 *
 * @var File|null
 */
protected $image;

/**
 * @ORM\Embedded(class="Vich\UploaderBundle\Entity\File")
 *
 * @var EmbeddedFile
 */
protected $imageFile;

public function __construct()

    $this->imageFile = new EmbeddedFile();


public function setImage(?File $image = null): void

    $this->image = $image;

    if (null !== $image) 
        // It is required that at least one field changes if you are using doctrine
        // otherwise the event listeners won't be called and the file is lost
        $this->updatedAt = new DateTimeImmutable();
    


public function getImage(): ?File

    return $this->image;


public function getImageFile(): ?EmbeddedFile

    return $this->imageFile;


public function setImageFile(EmbeddedFile $imageFile): self

    $this->imageFile = $imageFile;

    return $this;

【讨论】:

我仍然有消息:““App\Entity\Evenement”类中“setImage”方法中参数“image”的类型提示无效。” 我仍然有消息:“类“App\Entity\Evenement”中方法“setImage”中参数“image”的类型提示无效。”当我提交带有图片的表单时,我也有“给定对象不是声明此属性的类的实例”。有关表单构建器中的信息,有:->add('image', VichImageType::class, [...

以上是关于Symfony 5 VichUploaderBundler:类“App\Entity\xxx”中方法“setImageFile”中参数“imageFile”的类型提示无效的主要内容,如果未能解决你的问题,请参考以下文章

Symfony 5.2.3 接口类型提示

Symfony 5 邮件问题

Symfony 5 ApiKeyAuthenticator 和 SelfValidatingPassport

无法使用 Symfony 5 安装 FOSRestBundle

Symfony 5.1 弃用 RouteCollectionBuilder -> RoutingConfigurator

升级到 Symfony 5.3 并更新 flex 配方后出错 (symfony:recipes:install --force)