如何在Symfony5上使用带有easyAdmin3的Vich上传器

我一直在尝试使用 VichUploader 在 Symfony 项目上上传文件,已经在使用 EasyAdmin?3。

我已正确配置所有内容,但出现此错误:

“pieceJointeFile”图像字段必须定义使用 setUploadDir() 方法上传图像的目录。

<?php

namespace AppEntity;

use AppRepositoryInventaireRepository;
use DateTime;
use DoctrineORMMapping as ORM;
use SymfonyComponentHttpFoundationFileFile;
use VichUploaderBundleMappingAnnotation as Vich;

My entity

/**
 * @ORMEntity(repositoryClass=InventaireRepository::class)
 * @VichUploadable
 */
class Inventaire
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity=Conducteur::class, inversedBy="inventaires")
     */
    private $conducteur;

    /**
     * @ORMColumn(type="date")
     */
    private $dateInventaire;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $pieceJointe;

    /**
     * @VichUploadableField(mapping="pieceJointe", fileNameProperty="pieceJointe")
     */
    private $pieceJointeFile;

    /**
     * @ORMColumn(type="datetime")
     */
    private $updatedAt;

    public function __construct()
    {
        $this->updatedAt = new DateTime();
    }

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

    public function getConducteur(): ?Conducteur
    {
        return $this->conducteur;
    }

    public function setConducteur(?Conducteur $conducteur): self
    {
        $this->conducteur = $conducteur;

        return $this;
    }

    public function getDateInventaire(): ?DateTimeInterface
    {
        return $this->dateInventaire;
    }

    public function setDateInventaire(DateTimeInterface $dateInventaire): self
    {
        $this->dateInventaire = $dateInventaire;

        return $this;
    }

    public function getPieceJointeFile() {
        return $this->pieceJointeFile;
    }

    public function setPieceJointeFile($pieceJointeFile): void
    {
        $this->pieceJointeFile = $pieceJointeFile;

        if($pieceJointeFile) {
            $this->updatedAt = new DateTime();
        }
    }

    public function getPieceJointe() {
        return $this->pieceJointe;
    }


    public function setPieceJointe($pieceJointe):self
    {
        $this->pieceJointe = $pieceJointe;
        return $this;
    }

    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }
}

vich上传器配置

vich_uploader:
    db_driver: orm

    mappings:
        pieceJointe:
            uri_prefix: /files/pieceJointe
            upload_destination: '%kernel.project_dir%/public/files/pieceJointe'

最后是我的 crud 控制器

<?php

namespace AppControllerAdmin;

use AppEntityInventaire;
use EasyCorpBundleEasyAdminBundleConfigAction;
use EasyCorpBundleEasyAdminBundleConfigActions;
use EasyCorpBundleEasyAdminBundleConfigCrud;
use EasyCorpBundleEasyAdminBundleControllerAbstractCrudController;
use EasyCorpBundleEasyAdminBundleFieldAssociationField;
use EasyCorpBundleEasyAdminBundleFieldDateField;
use EasyCorpBundleEasyAdminBundleFieldImageField;
use EasyCorpBundleEasyAdminBundleFieldTextField;
use VichUploaderBundleFormTypeVichFileType;
use VichUploaderBundleFormTypeVichImageType;

class InventaireCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Inventaire::class;
    }


    public function configureFields(string $pageName): iterable
    {
        return [
            DateField::new('dateInventaire'),
            AssociationField::new('conducteur'),
            TextField::new('pieceJointeFile')->setFormType(VichFileType::class, [
                'delete_label' => 'supprimer?'
            ])->onlyOnForms(),
            ImageField::new('pieceJointe')->setBasePath('/files/pieceJointe')->onlyOnDetail(),
            ImageField::new('pieceJointeFile')->setFormType(VichImageType::class)
        ];
    }



    public function configureActions(Actions $actions): Actions
    {
        return $actions
            ->add(Crud::PAGE_INDEX, Action::DETAIL);
    }
}

最后,我想澄清一下,使用TextField它时可以正常工作。

回答

$imageFile = TextareaField::new('imageFile')->setFormType(VichImageType::class);

TextareaField到remplace的ImageField的,它的作品对我!!!!!

$imageFile = TextareaField

$image = ImageField


以上是如何在Symfony5上使用带有easyAdmin3的Vich上传器的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>