VichUploader: 媒体对象不可上传

huangapple go评论57阅读模式
英文:

VichUploader : MediaObject is not uploadable

问题

我尝试通过API平台上传文件:

我按照文档操作,但出现了以下错误:

"类 "App\\Entity\\MediaObject" 无法上传。如果您使用属性来配置VichUploaderBundle,您可能忘记在实体顶部添加 `#[Vich\\Uploadable]`。如果您不使用属性,请检查配置文件是否放置正确。在这两种情况下,清除缓存也可以解决问题。"

我使用的版本如下:

"api-platform/core": "^3.0",
"vich/uploader-bundle": "^2.0"

我的配置如下:

# api/config/packages/vich_uploader.yaml
vich_uploader:
  db_driver: orm
  metadata:
    type: attribute
  mappings:
    media_object:
      uri_prefix: /media
      upload_destination: '%kernel.project_dir%/public/media'
      namer: Vich\UploaderBundle\Naming\OrignameNamer

我的实体如下:

// api/src/Entity/MediaObject.php
namespace App\Entity;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata.ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use App\Controller.CreateMediaObjectAction;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

#[Vich\Uploadable]
#[ORM\Entity]
#[ApiResource(
    normalizationContext: ['groups' => ['media_object:read']], 
    types: ['https://schema.org/MediaObject'],
    operations: [
        new Get(),
        new GetCollection(),
        new Post(
            controller: CreateMediaObjectAction::class, 
            deserialize: false, 
            validationContext: ['groups' => ['Default', 'media_object_create']], 
            openapiContext: [
                'requestBody' => [
                    'content' => [
                        'multipart/form-data' => [
                            'schema' => [
                                'type' => 'object', 
                                'properties' => [
                                    'file' => [
                                        'type' => 'string', 
                                        'format' => 'binary'
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        )
    ]
)]
class MediaObject
{
    #[ORM\Id, ORM\Column, ORM\GeneratedValue]
    private ?int $id = null;

    #[ApiProperty(types: ['https://schema.org/contentUrl'])]
    #[Groups(['media_object:read'])]
    public ?string $contentUrl = null;

    #[Vich\UploadableField(mapping: "media_object", fileNameProperty: "filePath")]
    #[Assert\NotNull(groups: ['media_object_create'])]
    public ?File $file = null;

    #[ORM\Column(nullable: true)] 
    public ?string $filePath = null;

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

请注意,文档中的错误信息已经被翻译成中文。

英文:

I try to upload file throught a REST API with API Plateform

I followed the doc, but I got :

"The class \"App\\Entity\\MediaObject\" is not uploadable. If you use attributes to configure VichUploaderBundle, you probably just forgot to add `#[Vich\\Uploadable]` on top of your entity. If you don't use attributes, check that the configuration files are in the right place. In both cases, clearing the cache can also solve the issue.",

I'am using :

"api-platform/core": "^3.0",
"vich/uploader-bundle": "^2.0"

My config :

# api/config/packages/vich_uploader.yaml
vich_uploader:
db_driver: orm
metadata:
type: attribute
mappings:
media_object:
uri_prefix: /media
upload_destination: '%kernel.project_dir%/public/media'
namer: Vich\UploaderBundle\Naming\OrignameNamer

My entity :

<?php
// api/src/Entity/MediaObject.php
namespace App\Entity;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use App\Controller\CreateMediaObjectAction;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
#[Vich\Uploadable]
#[ORM\Entity]
#[ApiResource(
normalizationContext: ['groups' => ['media_object:read']], 
types: ['https://schema.org/MediaObject'],
operations: [
new Get(),
new GetCollection(),
new Post(
controller: CreateMediaObjectAction::class, 
deserialize: false, 
validationContext: ['groups' => ['Default', 'media_object_create']], 
openapiContext: [
'requestBody' => [
'content' => [
'multipart/form-data' => [
'schema' => [
'type' => 'object', 
'properties' => [
'file' => [
'type' => 'string', 
'format' => 'binary'
]
]
]
]
]
]
]
)
]
)]
class MediaObject
{
#[ORM\Id, ORM\Column, ORM\GeneratedValue]
private ?int $id = null;
#[ApiProperty(types: ['https://schema.org/contentUrl'])]
#[Groups(['media_object:read'])]
public ?string $contentUrl = null;
#[Vich\UploadableField(mapping: "media_object", fileNameProperty: "filePath")]
#[Assert\NotNull(groups: ['media_object_create'])]
public ?File $file = null;
#[ORM\Column(nullable: true)] 
public ?string $filePath = null;
public function getId(): ?int
{
return $this->id;
}
}

答案1

得分: 1

在我的情况下,这解决了问题:

composer require doctrine/annotations
英文:

In my case this resolved the problem:

composer require doctrine/annotations

huangapple
  • 本文由 发表于 2023年2月14日 00:24:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定