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

标签 php symfony doctrine-orm doctrine symfony5

我目前的实体代码有以下错误,我真的不明白为什么我会遇到这个问题。
我只在创建仪表板的页面中有它,而我不调用图像而只调用遗产,并且在表单中一切正常。
我试过改变 CollectionArrayCollection但一切都没有改变。
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;
    }
}

最佳答案

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

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

关于php - Symfony: $images 必须是 Doctrine\Common\Collections\ArrayCollection 的一个实例,Doctrine\ORM\PersistentCollection 使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65762117/

相关文章:

php - 如何在 PHP 中使用 var_dump() 查看长字符串的全部内容

mapping - 有没有一种方法可以在配置文件之外修改该学说的实体映射配置?

testing - 学说 2 : testing repository classes with YAML config

php - 选择 Doctrine 和 MS SQL 2008 的问题

php - 我真的可以对致命的解析错误无所事事吗?

php - 插入批处理 codeigniter 后重定向不起作用

javascript - 从表单在同一页面内调用 JS 函数(AJAX)

php - Symfony2 和 FOSUserBundle : Override Mailer

php - Symfony3 尝试使用表单和 Twig 保存到数据库中

symfony - 如何在 YAML 中使用另一个参数内部的参数