【问题标题】:Symfony: $images must be an instance of Doctrine\Common\Collections\ArrayCollection, Doctrine\ORM\PersistentCollection usedSymfony:$images 必须是 Doctrine\Common\Collections\ArrayCollection 的实例,使用 Doctrine\ORM\PersistentCollection
【发布时间】:2021-04-22 00:26:45
【问题描述】:

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

我只在我的创建仪表板的页面中拥有它,而我不调用图像而只调用遗产,并且在表单中一切正常。

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

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

【问题讨论】:

  • 请分享更多细节,例如重现问题所需的代码

标签: php symfony doctrine-orm doctrine symfony5


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我才发现 $images 打错了,是“ArrayCollection”,而一定是“Collection”

    【讨论】:

      猜你喜欢
      • 2019-07-13
      • 1970-01-01
      • 2018-03-21
      • 1970-01-01
      • 2016-05-05
      • 2018-01-22
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多