VichUploaderBundle в Symfony 6

Я надеюсь, что вы можете мне помочь, потому что я ищу, и я потерялся :(

Я пытаюсь загрузить изображение в свой проект Symfony 6 с помощью VichUploaderBundle.

Я использовал документ: https://github.com/dustin10/VichUploaderBundle/blob/master/docs/usage.md#step-1-configure-an-upload-mapping

Но у меня есть эта ошибка:

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

Мой vich_uploader.yaml:

vich_uploader:
    db_driver: orm


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

Моя клиентская сущность:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\Repository\ClientRepository;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\HttpFoundation\File\File;
use Doctrine\Common\Collections\ArrayCollection;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

#[ORM\Entity(repositoryClass: ClientRepository::class)]
#[Vich\Uploadable]
class Client
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private $id;

    #[ORM\Column(type: 'integer')]
    private $numero_client;

    /**
    * Some fields
    */

    /**
     * NOTE: This is not a mapped field of entity metadata, just a simple property.
     */
    #[Vich\UploadableField(mapping: 'clients_logo', fileNameProperty: 'logo')]
    private ?File $imageFile = null;

    #[ORM\Column(type: 'string', length: 255, nullable: true)]
    private $logo;


    #[ORM\Column(type: 'datetime', nullable: true)]
    private ?\DateTimeInterface $updatedAt = null;

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

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

    public function getLogo(): ?string
    {
        return $this->logo;
    }

    public function setLogo(?string $logo): self
    {
        $this->logo = $logo;

        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 getBddClient(): ?BaseClient
    {
        return $this->bdd_client;
    }

    public function setBddClient(?BaseClient $bdd_client): self
    {
        $this->bdd_client = $bdd_client;

        return $this;
    }

    /**
     * @return Collection<int, Applis>
     */
    public function getApplis(): Collection
    {
        return $this->applis;
    }

    public function addAppli(Applis $appli): self
    {
        if (!$this->applis->contains($appli)) {
            $this->applis[] = $appli;
        }

        return $this;
    }

    public function removeAppli(Applis $appli): self
    {
        $this->applis->removeElement($appli);

        return $this;
    }

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

    public function setUpdateAt(): self
    {
        $this->updatedAt = new \DateTimeImmutable();

        return $this;
    }
}

Мой тип формы:

<?php

namespace App\Form;

use App\Entity\Client;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\FormBuilderInterface;
use Vich\UploaderBundle\Form\Type\VichImageType;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

class ClientType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('nom', TextType::class, [
                'label' => false,
            ])
            //Some add fields
            ->add('imageFile', VichImageType::class, [
                'required' => false
            ]);

        //Indispensable pour faire fonctionner le select du formulaire sinon erreur 500
        $builder->get('type_contrat')
            ->addModelTransformer(new CallbackTransformer(
                function ($typeContratArray) {
                    // transform the array to a string
                    return count($typeContratArray) ? $typeContratArray[0] : null;
                },
                function ($typeContratString) {
                    // transform the string back to an array
                    return [$typeContratString];
                }
            ));
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Client::class,
        ]);
    }
}

В моей форме ветка:

<div class = "col-md-6">
    {{ form_row(form.imageFile, { 
        label: 'Logo',
        'attr': {'class': 'form-control'}
    }) }}
</div>

Кто-нибудь может объяснить, что происходит? :(

Стоит ли изучать PHP в 2026-2027 годах?
Стоит ли изучать PHP в 2026-2027 годах?
Привет всем, сегодня я хочу высказать свои соображения по поводу вопроса, который я уже много раз получал в своем сообществе: "Стоит ли изучать PHP в...
Symfony Station Communiqué - 7 июля 2023 г
Symfony Station Communiqué - 7 июля 2023 г
Это коммюнике первоначально появилось на Symfony Station .
Оживление вашего приложения Laravel: Понимание режима обслуживания
Оживление вашего приложения Laravel: Понимание режима обслуживания
Здравствуйте, разработчики! В сегодняшней статье мы рассмотрим важный аспект управления приложениями, который часто упускается из виду в суете...
Установка и настройка Nginx и PHP на Ubuntu-сервере
Установка и настройка Nginx и PHP на Ubuntu-сервере
В этот раз я сделаю руководство по установке и настройке nginx и php на Ubuntu OS.
Коллекции в Laravel более простым способом
Коллекции в Laravel более простым способом
Привет, читатели, сегодня мы узнаем о коллекциях. В Laravel коллекции - это способ манипулировать массивами и играть с массивами данных. Благодаря...
Как установить PHP на Mac
Как установить PHP на Mac
PHP - это популярный язык программирования, который используется для разработки веб-приложений. Если вы используете Mac и хотите разрабатывать...
0
0
63
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Предполагая, что вы используете Php 8+, настройте пакет для использования атрибуты вместо аннотации.

vich_uploader:
    metadata:
        type: attribute

ссылка документы

О чувак, спасибо большое! Теперь все в порядке! Я так устала, что не увидела эту строчку в доке ?

Christopher CARVALHO 01.04.2022 09:44

Другие вопросы по теме