微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用EasyAdmin 3.0和VichUploader在管理控制台中显示图像?

如何解决如何使用EasyAdmin 3.0和VichUploader在管理控制台中显示图像?

我有一个带有图片的实体,并且我正在使用EasyAdmin 3.0和VichUploader 1.14在管理控制台中上传图片。我的问题是我无法在管理控制台中显示图像。我怎样才能做到这一点 ? 这是我的实体:

class Question
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     * @Groups({"question"})
     */
    private $id;

    /**
     * @ORM\Column(type="text",nullable=true)
     * @Groups({"question"})
     */
    private $title;

    /**
     * NOTE: This is not a mapped field of entity Metadata,just a simple property.
     *
     * @Vich\UploadableField(mapping="stand_image",fileNameProperty="imageName",size="imageSize")
     *
     * @var File|null
     */
    private $imageFile;

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

    /**
     * @ORM\Column(type="integer",nullable=true)
     *
     * @var int|null
     */
    private $imageSize;

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

    /**
     * @ORM\Column(type="string",length=255)
     * @Groups({"question"})
     */
    private $answer;

    /**
     * @ORM\Column(type="text",nullable=true)
     * @Groups({"question"})
     */
    private $explanation;

    /**
     * @ORM\Column(type="integer",nullable=true)
     * @Groups({"question"})
     */
    private $level;

    /**
     * @ORM\OnetoMany(targetEntity=Answer::class,mappedBy="question")
     * @Groups({"question"})
     */
    private $answers;

    /**
     * @ORM\ManyToOne(targetEntity=Game::class,inversedBy="questions")
     * @Groups({"question"})
     */
    private $game;

    /**
     * @ORM\OnetoMany(targetEntity=Proposal::class,mappedBy="question")
     * @Groups({"question"})
     */
    private $proposals;

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

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

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

    public function setTitle(string $title): self
    {
        $this->title = $title;

        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;
    }

    public function setimageSize(?int $imageSize): void
    {
        $this->imageSize = $imageSize;
    }

    public function getimageSize(): ?int
    {
        return $this->imageSize;
    }

    public function getAnswer(): ?string
    {
        return $this->answer;
    }

    public function setAnswer(string $answer): self
    {
        $this->answer = $answer;

        return $this;
    }

    public function getExplanation(): ?string
    {
        return $this->explanation;
    }

    public function setExplanation(?string $explanation): self
    {
        $this->explanation = $explanation;

        return $this;
    }

    public function getLevel(): ?int
    {
        return $this->level;
    }

    public function setLevel(?int $level): self
    {
        $this->level = $level;

        return $this;
    }

    /**
     * @return Collection|Answer[]
     */
    public function getAnswers(): Collection
    {
        return $this->answers;
    }

    public function addAnswer(Answer $answer): self
    {
        if (!$this->answers->contains($answer)) {
            $this->answers[] = $answer;
            $answer->setQuestion($this);
        }

        return $this;
    }

    public function removeAnswer(Answer $answer): self
    {
        if ($this->answers->contains($answer)) {
            $this->answers->removeElement($answer);
            // set the owning side to null (unless already changed)
            if ($answer->getQuestion() === $this) {
                $answer->setQuestion(null);
            }
        }

        return $this;
    }

    public function getGame(): ?Game
    {
        return $this->game;
    }

    public function setGame(?Game $game): self
    {
        $this->game = $game;

        return $this;
    }

    /**
     * @return Collection|Proposal[]
     */
    public function getProposals(): Collection
    {
        return $this->proposals;
    }

    public function addProposal(Proposal $proposal): self
    {
        if (!$this->proposals->contains($proposal)) {
            $this->proposals[] = $proposal;
            $proposal->setQuestion($this);
        }

        return $this;
    }

    public function removeProposal(Proposal $proposal): self
    {
        if ($this->proposals->contains($proposal)) {
            $this->proposals->removeElement($proposal);
            // set the owning side to null (unless already changed)
            if ($proposal->getQuestion() === $this) {
                $proposal->setQuestion(null);
            }
        }

        return $this;
    }

    public function __toString()
    {
        return "{$this->title}";
    }
}

这是我的vich_uploader.yaml文件

vich_uploader:
    db_driver: orm

    mappings:
        question_image:
            uri_prefix: /images/questions
            upload_destination: '%kernel.project_dir%/public/images/questions'
            namer: Vich\UploaderBundle\Naming\OrignameNamer

这是我的EasyAdmin的CRUD控制器:

class QuestionCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Question::class;
    }

    public function configureCrud(Crud $crud): Crud
    {
        return $crud
            // Les labels utilisés pour faire référence à l'entité dans les titres,les boutons,etc.
            ->setEntityLabelInSingular('question')
            ->setEntityLabelInPlural('questions')

            // Le titre visible en haut de la page et le contenu de l'élément <title>
            // Cela peut inclure ces différents placeholders : %entity_id%,%entity_label_singular%,%entity_label_plural%
            ->setPageTitle('index','Liste des %entity_label_plural%')
            ->setPageTitle('new','Créer une %entity_label_singular%')
            ->setPageTitle('edit','Modifier la %entity_label_singular% <small>(#%entity_id%)</small>')

            // Définit le tri initial appliqué à la liste
            // (l'utilisateur peut ensuite modifier ce tri en cliquant sur les colonnes de la table)
            ->setDefaultSort(['id' => 'ASC'])
            ;
    }

    public function configureActions(Actions $actions): Actions
    {
        return $actions
            ->update(Crud::PAGE_INDEX,Action::NEW,function (Action $action) {
                return $action->setLabel('Ajouter une %entity_label_singular%');
            })
            ->update(Crud::PAGE_INDEX,Action::EDIT,function (Action $action) {
                return $action->setLabel('Modifier');
            })
            ->update(Crud::PAGE_INDEX,Action::DELETE,function (Action $action) {
                return $action->setLabel('Supprimer');
            })
            ->update(Crud::PAGE_NEW,Action::SAVE_AND_RETURN,function (Action $action) {
                return $action->setLabel('Créer');
            })
            ->update(Crud::PAGE_NEW,Action::SAVE_AND_ADD_ANOTHER,function (Action $action) {
                return $action->setLabel('Créer et ajouter une autre %entity_label_singular%');
            })
            ->update(Crud::PAGE_EDIT,function (Action $action) {
                return $action->setLabel('Sauvegarder les changements');
            })
            ->update(Crud::PAGE_EDIT,Action::SAVE_AND_CONTINUE,function (Action $action) {
                return $action->setLabel('Sauvegarder et continuer à modifier');
            })
            ->update(Crud::PAGE_DETAIL,function (Action $action) {
                return $action->setLabel('Supprimer');
            })
            ->update(Crud::PAGE_DETAIL,Action::INDEX,function (Action $action) {
                return $action->setLabel('Retour à la liste');
            })
            ->update(Crud::PAGE_DETAIL,function (Action $action) {
                return $action->setLabel('Modifier');
            })
            ;
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            IdField::new('id','Id')->hideOnForm(),TextField::new('title','Intitulé'),ImageField::new('imageFile','Image')
            ->setFormType(VichImageType::class)
            ->setBasePath('/public/images/questions'),TextField::new('answer','Réponse'),TextField::new('explanation','Explications'),IntegerField::new('level','Niveau'),AssociationField::new('game','Jeu'),];
    }
}

Here's the result in my admin console. What I'd want is that the image appears instead of null.

解决方法

您必须仅在“表单”页面上显示imageFile(带有-> onlyOnForms()),而在索引页面上必须显示imageName(带有onlyOnIndex()),并且设置与VichUploader配置中相同的路径(/ images / questions)

请参见以下示例:

public function configureFields(string $pageName): iterable
{
    return [
        ImageField::new('imageName','Image')
            ->onlyOnIndex()
            ->setBasePath('/images/questions'),ImageField::new('imageFile','Image')
            ->onlyOnForms()
            ->setFormType(VichImageType::class)
    ];
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。