Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony: $images must be an instance of Doctrine\Common\Collections\ArrayCollection, Doctrine\ORM\PersistentCollection used

I currently have the following error with my entity code, and I don't really understand why I am having this problem.

I only have it in the page of my create dashboard, while I don't call the images but only the heritages, and in the form everything works fine.

I have tried changing Collection to ArrayCollection but nothing has changed.

Error encountered

<?php

namespace App\Entity;

use App\Repository\PatrimoineRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass=PatrimoineRepository::class)
 */
class Patrimoine
{
    /**
     * @ORM\Id
     * @ORM\Column(type="string", unique=true)
     * @ORM\GeneratedValue("UUID")
     */
    private ?string $id = null;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private ?string $name;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private ?string $description;

    /**
     * @ORM\Column(type="json")
     * @Assert\Collection(
     *     fields={
     *          "lat" = {
     *              @Assert\NotBlank,
     *              @Assert\Regex("/^(-?(?:1[0-7]|[1-9])?\d(?:\.\d{1,18})?|180(?:\.0{1,18})?)$/")
     *          },
     *          "lng" = {
     *              @Assert\NotBlank,
     *              @Assert\Regex("/^(-?[1-8]?\d(?:\.\d{1,18})?|90(?:\.0{1,18})?)$/")
     *          },
     *    },
     *    missingFieldsMessage="Le champs {{ field }} est manquant"
     * )
     */
    private array $localisation = [];

    /**
     * @ORM\Column(type="datetime_immutable")
     */
    private ?\DateTimeImmutable $created_at;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private ?string $statut;

    /**
     * @ORM\ManyToOne(targetEntity=CategoryPatrimoine::class, inversedBy="patrimoines")
     * @ORM\JoinColumn(nullable=false)
     */
    private ?CategoryPatrimoine $category;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private ?string $visibility;

    /**
     * @ORM\ManyToOne(targetEntity=User::class, inversedBy="patrimoines")
     * @ORM\JoinColumn(nullable=false)
     */
    private ?User $author;

    /**
     * @ORM\OneToMany(targetEntity=Image::class, mappedBy="patrimoine", orphanRemoval=true)
     */
    private ArrayCollection $images;

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

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

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(string $description): self
    {
        $this->description = $description;

        return $this;
    }

    public function getLocalisation(): ?array
    {
        return $this->localisation;
    }

    public function setLocalisation(array $localisation): self
    {
        $this->localisation = $localisation;

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeImmutable
    {
        return $this->created_at;
    }

    public function setCreatedAt(\DateTimeImmutable $created_at): self
    {
        $this->created_at = $created_at;

        return $this;
    }

    public function getStatut(): ?string
    {
        return $this->statut;
    }

    public function setStatut(string $statut): self
    {
        $this->statut = $statut;

        return $this;
    }

    public function getCategory(): ?CategoryPatrimoine
    {
        return $this->category;
    }

    public function setCategory(?CategoryPatrimoine $category): self
    {
        $this->category = $category;

        return $this;
    }

    public function getVisibility(): ?string
    {
        return $this->visibility;
    }

    public function setVisibility(string $visibility): self
    {
        $this->visibility = $visibility;

        return $this;
    }

    public function getAuthor(): ?User
    {
        return $this->author;
    }

    public function setAuthor(?User $author): self
    {
        $this->author = $author;

        return $this;
    }

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

    public function addImage(Image $image): self
    {
        if (!$this->images->contains($image)) {
            $this->images[] = $image;
            $image->setPatrimoine($this);
        }

        return $this;
    }

    public function removeImage(Image $image): self
    {
        // set the owning side to null (unless already changed)
        if ($this->images->removeElement($image) && $image->getPatrimoine() === $this) {
            $image->setPatrimoine(null);
        }

        return $this;
    }
}
like image 219
AlexVWeb Avatar asked Jan 24 '23 12:01

AlexVWeb


2 Answers

I faced a similar problem too. You must declare the property as Collection (interface), not as implementation ArrayCollection because Doctrine ORM uses PersistentCollection when picks up data from database but you declare the property as ArrayCollection, and type mismatch happens.

class SomeEntity {
    private Collection $items;
    // all other code
}
like image 186
Dmitry Vapelnik Avatar answered Jan 27 '23 02:01

Dmitry Vapelnik


I just realized that the typing of $ images was wrong, it was "ArrayCollection" whereas it must be "Collection"

like image 38
AlexVWeb Avatar answered Jan 27 '23 03:01

AlexVWeb