I’m following VichUploader instructions to add a file upload to my form for the Item entity.
Now, if I go to any page of the site I get a raw error (without Symfony profiler and not pretty printed) about two entity names already in use :
Fatal error: Cannot declare class AppEntityItemCategory, because the name is already in use in /Users/corentoulf/DEV/boite-a-partage/src/Entity/ItemCategory.php on line 11.
Fatal error: Cannot declare class AppEntityItemType, because the name is already in use in /Users/corentoulf/DEV/boite-a-partage/src/Entity/ItemType.php on line 11
If I go to the page where the concerned form is, I get a raw error + pretty Symofny error about only the ItemType entity:
Fatal error: Cannot declare class AppEntityItemType, because the name is already in use in /Users/corentoulf/DEV/boite-a-partage/src/Entity/ItemType.php on line 11
What am I missing in VichUploader configuration that causes such errors ?
Configuration
Vich version : 2.7.0
Symfony version : 7.3.1
PHP version : 8.3.6
Vich config file
vich_uploader:
db_driver: orm
metadata:
type: attribute
auto_detection: true
mappings:
items:
uri_prefix: /images/items
upload_destination: '%kernel.project_dir%/public/images/items'
namer: VichUploaderBundleNamingSmartUniqueNamer
Item Entity
namespace AppEntity;
use AppRepositoryItemRepository;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use SymfonyComponentHttpFoundationFileFile;
use VichUploaderBundleMappingAnnotation as Vich;
#[ORMEntity(repositoryClass: ItemRepository::class)]
#[VichUploadable]
class Item
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn]
private ?int $id = null;
#[ORMManyToOne(inversedBy: 'items')]
#[ORMJoinColumn(nullable: false)]
private ?User $owner = null;
#[ORMColumn]
private ?DateTimeImmutable $created_at = null;
/**
* @var Collection<int, ItemCircle>
*/
#[ORMOneToMany(targetEntity: ItemCircle::class, mappedBy: 'item', cascade: ['persist'], orphanRemoval: true)]
private Collection $itemCircles;
#[ORMManyToOne(inversedBy: 'items')]
private ?itemType $itemType = null;
#[ORMColumn(length: 255, nullable: true)]
private ?string $property_1 = null;
#[ORMColumn(length: 255, nullable: true)]
private ?string $property_2 = null;
#[ORMColumn(length: 255, nullable: true)]
private ?string $property_3 = null;
#[ORMColumn(length: 255, nullable: true)]
private ?string $property_4 = null;
#[ORMColumn(length: 255, nullable: true)]
private ?string $property_5 = null;
// NOTE: This is not a mapped field of entity metadata, just a simple property.
#[VichUploadableField(mapping: 'items', fileNameProperty: 'imageName', size: 'imageSize', mimeType: "imageMimeType")]
private ?File $imageFile = null;
#[ORMColumn(nullable: true)]
private ?string $imageName = null;
#[ORMColumn(nullable: true)]
private ?int $imageSize = null;
#[ORMColumn(nullable: true)]
private ?int $imageMimeType = null;
#[ORMColumn(nullable: true)]
private ?DateTimeImmutable $updated_at = null;
/**
* 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|SymfonyComponentHttpFoundationFileUploadedFile|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->updated_at = new DateTimeImmutable();
}
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImageName(?string $imageName): void
{
$this->imageName = $imageName;
}
public function getImageMimeType(): ?string
{
return $this->imageMimeType;
}
public function setImageMimeType(?string $imageMimeType): void
{
$this->imageMimeType = $imageMimeType;
}
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 __construct()
{
$this->itemCircles = new ArrayCollection();
}
public function __toString()
{
return $this->property_1;
}
public function getId(): ?int
{
return $this->id;
}
public function getOwner(): ?User
{
return $this->owner;
}
public function setOwner(?User $owner): static
{
$this->owner = $owner;
return $this;
}
public function getCreatedAt(): ?DateTimeImmutable
{
return $this->created_at;
}
public function setCreatedAt(DateTimeImmutable $created_at): static
{
$this->created_at = $created_at;
return $this;
}
/**
* @return Collection<int, ItemCircle>
*/
public function getItemCircles(): Collection
{
return $this->itemCircles;
}
public function addItemCircle(ItemCircle $itemCircle): static
{
if (!$this->itemCircles->contains($itemCircle)) {
$this->itemCircles->add($itemCircle);
$itemCircle->setItem($this);
}
return $this;
}
public function removeItemCircle(ItemCircle $itemCircle): static
{
if ($this->itemCircles->removeElement($itemCircle)) {
// set the owning side to null (unless already changed)
if ($itemCircle->getItem() === $this) {
$itemCircle->setItem(null);
}
}
return $this;
}
public function getItemType(): ?itemType
{
return $this->itemType;
}
public function setItemType(?itemType $itemType): static
{
$this->itemType = $itemType;
return $this;
}
public function getProperty1(): ?string
{
return $this->property_1;
}
public function setProperty1(?string $property_1): static
{
$this->property_1 = $property_1;
return $this;
}
public function getProperty2(): ?string
{
return $this->property_2;
}
public function setProperty2(?string $property_2): static
{
$this->property_2 = $property_2;
return $this;
}
public function getProperty3(): ?string
{
return $this->property_3;
}
public function setProperty3(?string $property_3): static
{
$this->property_3 = $property_3;
return $this;
}
public function getProperty4(): ?string
{
return $this->property_4;
}
public function setProperty4(?string $property_4): static
{
$this->property_4 = $property_4;
return $this;
}
public function getProperty5(): ?string
{
return $this->property_5;
}
public function setProperty5(?string $property_5): static
{
$this->property_5 = $property_5;
return $this;
}
}