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

5

我的实体代码出现了以下错误,但我真的不明白为什么会有这个问题。

这个错误只出现在我的创建面板页面中,虽然我只调用遗产而不是图片,在表单中一切工作正常。

我尝试将 Collection 更改为 ArrayCollection,但没有任何改变。

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

请分享更多细节,例如重现问题所需的代码。 - Nico Haase
2个回答

17

我也遇到了类似的问题。你必须将属性声明为Collection(接口),而不是作为实现ArrayCollection,因为当Doctrine ORM从数据库中提取数据时会使用PersistentCollection,但你却将属性声明为ArrayCollection,从而导致类型不匹配。

class SomeEntity {
    private Collection $items;
    // all other code
}

3

我刚刚意识到$ images的输入错误,应该是“Collection”而不是“ArrayCollection”


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接