Symfony: $images 必须是 Doctrine\Common\Collections\ArrayCollection 的实例,使用 Doctrine\ORM\PersistentCollection

Ale*_*Web 5 php doctrine symfony doctrine-orm symfony5

目前,我的实体代码出现以下错误,我不太明白为什么会遇到这个问题。

我只将它放在创建仪表板的页面中,而我不调用图像,而只调用遗产,并且在形式上一切正常。

我尝试过更改CollectionArrayCollection但没有任何改变。

遇到错误

<?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;
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 17

我也遇到了类似的问题。您必须将属性声明为Collection(接口),而不是实现ArrayCollection,因为 Doctrine ORMPersistentCollection在从数据库获取数据时使用,但您将属性声明为ArrayCollection,并且会发生类型不匹配。

class SomeEntity {
    private Collection $items;
    // all other code
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*Web 3

我刚刚意识到 $ images 的输入是错误的,它是“ArrayCollection”,而它必须是“Collection”