You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony 6中VichUploaderBundle提示Uploadable属性类未找到求助

VichUploaderBundle 报错:Attribute class "Vich\uploaderBundle\Mapping\Annotation\Uploadable" not found

错误信息

Attribute class "Vich\uploaderBundle\Mapping\Annotation\Uploadable" not found

配置文件 vich_uploader.yaml

vich_uploader:
    db_driver: orm
    metadata:
        type: attribute

    mappings:
        products:
            uri_prefix: /images/products
            upload_destination: '%kernel.project_dir%/public/images/products'
            namer: Vich\UploaderBundle\Naming\SmartUniqueNamer

Produits 实体代码

<?php

namespace App\Entity;

use App\Repository\ProduitsRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use phpDocumentor\Reflection\Types\True_;
use Vich\uploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\HttpFoundation\File\File;


#[ORM\Entity(repositoryClass: ProduitsRepository::class)]
#[Vich\Uploadable]
class Produits
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 100)]
    private ?string $nom = null;

    #[ORM\Column(type: Types::TEXT)]
    private ?string $description = null;

    #[ORM\Column(type: Types::SMALLINT)]
    private ?int $star = null;

    #[ORM\Column]
    private ?int $prix = null;

    #[ORM\Column]
    private ?bool $is_publish;

    #[Vich\UploadableField(mapping: 'products', fileNameProperty: 'imageName', size: 'imageSize')]
    private ?File $imageFile = null;

    #[ORM\Column(nullable: True)]
    private ?string $imageName = null;

    #[ORM\Column(nullable: true)]
    private ?\DateTimeImmutable $Update_At = null;

    #[ORM\Column]
    private ?\DateTimeImmutable $Create_At;

    public function __construct()
    {
        $this->Create_At = new \DateTimeImmutable();
        $this->is_publish = false;
    }

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

    public function getNom(): ?string
    {
        return $this->nom;
    }

    public function setNom(string $nom): self
    {
        $this->nom = $nom;

        return $this;
    }

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

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

        return $this;
    }

    public function getStar(): ?int
    {
        return $this->star;
    }

    public function setStar(int $star): self
    {
        $this->star = $star;

        return $this;
    }

    public function getPrix(): ?int
    {
        return $this->prix;
    }

    public function setPrix(int $prix): self
    {
        $this->prix = $prix;

        return $this;
    }

    public function isIsPublish(): ?bool
    {
        return $this->is_publish;
    }

    public function setIsPublish(bool $is_publish): self
    {
        $this->is_publish = $is_publish;

        return $this;
    }

    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->Update_At = new \DateTimeImmutable();
        }
    }

    public function getImageFile(): ?File
    {
        return $this->imageFile;
    }
    public function getImageName(): ?string
    {
        return $this->imageName;
    }

    public function setImageName(string $imageName): self
    {
        $this->imageName = $imageName;

        return $this;
    }

    public function getUpdateAt(): ?\DateTimeImmutable
    {
        return $this->Update_At;
    }

    public function setUpdateAt(?\DateTimeImmutable $Update_At): static
    {
        $this->Update_At = $Update_At;

        return $this;
    }

    public function getCreateAt(): ?\DateTimeImmutable
    {
        return $this->Create_At;
    }

    public function setCreateAt(\DateTimeImmutable $Create_At): static
    {
        $this->Create_At = $Create_At;

        return $this;
    }
}

已执行的排查操作

  • 执行 symfony console cache:clear 清理缓存
  • 确认 bundles.php 中已注册 VichUploaderBundle:Vich\UploaderBundle\VichUploaderBundle::class => ['all' => true],
  • 更新了项目依赖

解决方案

1. 确认 VichUploaderBundle 版本兼容性

属性(Attribute)支持是 VichUploaderBundle 2.0+ 版本才引入的,如果你的版本低于2.0,要么升级到最新版本,要么改用注解(Annotation)方式配置。

升级命令:

composer require vich/uploader-bundle

2. 修正命名空间与属性用法

如果已经是2.0+版本,当前实体导入的是注解命名空间,需要替换为属性专用的命名空间:

// 替换原有的 use 语句
use Vich\UploaderBundle\Attribute\Vich as Vich;

同时调整属性参数:属性版本的 UploadableField 不再支持 size 参数,需要移除该参数(如需存储文件大小,需自行添加字段并手动处理)。修正后的实体关键代码:

#[ORM\Entity(repositoryClass: ProduitsRepository::class)]
#[Vich\Uploadable]
class Produits
{
    // ...

    #[Vich\UploadableField(mapping: 'products', fileNameProperty: 'imageName')]
    private ?File $imageFile = null;

    // ...
}

3. 验证配置与清理缓存

确认 vich_uploader.yaml 中 metadata: type: attribute 的配置保持不变,再次执行缓存清理:

symfony console cache:clear

内容的提问来源于stack exchange,提问作者ulrich soo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 13:25:27