Symfony 6 EasyAdmin 4 VichUploader多文件

66bbxpm5  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(100)

你能帮助我如何上传文件使用Symfony 6和EasyAdmin 4?
我有:

文件

#[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ApiProperty]
    public ?string $contentUrl;

    #[ApiProperty]
    public ?string $size;

    #[Assert\NotNull]
    #[Assert\NotBlank]
    #[Assert\File(
        maxSize: "10M",
        mimeTypes: ["image/png", "image/jpg", "image/jpeg"],
        maxSizeMessage: "Too big file, 10M is max.",
        mimeTypesMessage: "Please use only images formats - png, jpj, jpeg",
    )]
    #[Vich\UploadableField(mapping: "file", fileNameProperty: "filePath")]
    public ?UploadedFile $file = null;

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

    public function __construct(
        UploadedFile $file
    )
    {
        $this->file = $file;
    }

    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @return string|null
     */
    public function getContentUrl(): ?string
    {
        return $this->contentUrl;
    }

    /**
     * @param string|null $contentUrl
     */
    public function setContentUrl(?string $contentUrl): void
    {
        $this->contentUrl = $contentUrl;
    }

    /**
     * @return string|null
     */
    public function getSize(): ?string
    {
        return $this->size;
    }

    /**
     * @param string|null $size
     */
    public function setSize(?string $size): void
    {
        $this->size = $size;
    }

    /**
     * @return UploadedFile|null
     */
    public function getFile(): ?UploadedFile
    {
        return $this->file;
    }

    /**
     * @param UploadedFile|null $file
     */
    public function setFile(?UploadedFile $file): void
    {
        $this->file = $file;
    }

    /**
     * @return string|null
     */
    public function getFilePath(): ?string
    {
        return $this->filePath;
    }

    /**
     * @param string|null $filePath
     */
    public function setFilePath(?string $filePath): void
    {
        $this->filePath = $filePath;
    }

项目镜像

#[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: "Project", cascade: ["ALL"], inversedBy: "images")]
    #[ORM\JoinColumn(name: "project", referencedColumnName: "id", onDelete: "CASCADE")]
    #[Assert\NotNull]
    private Project $project;

    #[ORM\ManyToOne(targetEntity: "File", cascade: ["ALL"], fetch: "EAGER")]
    #[ORM\JoinColumn(name: "image", referencedColumnName: "id", onDelete: "CASCADE")]
    private File $image;

    public function __construct(
        Project $project,
        File   $image
    )
    {
        $this->project = $project;
        $this->image = $image;
    }

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

    /**
     * @return Project|null
     */
    public function getProject(): ?Project
    {
        return $this->project;
    }

    /**
     * @param Project|null $project
     */
    public function setProject(?Project $project): void
    {
        $this->project = $project;
    }

    /**
     * @return File
     */
    public function getImage(): File
    {
        return $this->image;
    }

    /**
     * @param File $image
     */
    public function setImage(File $image): void
    {
        $this->image = $image;
    }

项目

#[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(type: "string")]
    #[Assert\NotBlank()]
    #[Assert\NotNull()]
    private string $title;

    #[ORM\Column(type: "text")]
    #[Assert\NotBlank()]
    #[Assert\NotNull()]
    private string $text;

    #[ORM\OneToMany(mappedBy: "project", targetEntity: "ProjectImage")]
    private ?Collection $images;

    public function __construct(
    )
    {
        $this->images = new ArrayCollection();
    }

    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

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

    /**
     * @param string $title
     */
    public function setTitle(string $title): void
    {
        $this->title = $title;
    }

    /**
     * @return string
     */
    public function getText(): string
    {
        return $this->text;
    }

    /**
     * @param string $text
     */
    public function setText(string $text): void
    {
        $this->text = $text;
    }

    /**
     * @return Collection|null
     */
    public function getImages(): ?Collection
    {
        return $this->images;
    }

    /**
     * @param Collection|null $images
     */
    public function setImages(?Collection $images): void
    {
        $this->images = $images;
    }

    public function addImage(?ProjectImage $image): void
    {
        $this->images->add($image);
    }

    public function resetImage(): void
    {
        $this->images = new ArrayCollection();
    }```

**vich_uploader**

vich_uploader:db_driver:orm

mappings:
    file:
        uri_prefix: /images/files
        upload_destination: '%kernel.project_dir%/var/files'
        namer: Vich\UploaderBundle\Naming\OrignameNamer
        directory_namer:
            service: Vich\UploaderBundle\Naming\CurrentDateTimeDirectoryNamer
            options:
                date_time_format: 'Y/m/d'
                date_time_property: uploadedTimestamp
        delete_on_remove: true
**ImagesType**
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('images', VichImageType::class,
        [
        'multiple' => true,
        'empty_data' => '',
        'required' => false,
        'data_class' => null
        ]
    );
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults([
        'data_class' => Project::class,
        "allow_extra_fields" => true,
    ]);
}```

ProjectCrudController

public function configureFields(string $pageName): iterable
    {
        return [
            IdField::new('id')->hideOnForm()->hideOnIndex(),
            IdField::new('uuid')->hideOnForm()->hideOnIndex(),
            TextField::new('title'),
            TextEditorField::new('text'),
            ImageField::new('images')->setBasePath('/images/files')->setUploadDir('/var/files'),
            TextareaField::new('images')->setFormType(VichImageType::class)
        ];
    }

我有类“应用\实体\项目”是不可上传的.enter image description here请告诉我,我做错了什么.
尝试使用FormType添加新字段,替换为其他类型

tvokkenx

tvokkenx1#

您是否已将“可上传”属性添加到项目实体中?

#[Vich\Uploadable]
class Project
...

相关问题