【问题标题】:Found entity of type Doctrine\Common\Collections\ArrayCollection on association but expecting在关联中找到类型为 Doctrine\Common\Collections\ArrayCollection 的实体,但期待
【发布时间】:2015-08-12 15:21:06
【问题描述】:

我在symfony2 / doctrine 中遇到与一对多自引用关系相关的问题。此处描述了类似的问题:Error Symfony2 Doctrine Found entity of type Doctrine\Common\Collections\ArrayCollection on association sth#vote, but expecting sth\Vote 但未提供解决方案。

我去以下异常:

找到类型为 Doctrine\Common\Collections\ArrayCollection 的实体 关联 AppBundle\Entity\Kategoria#corka,但期待 AppBundle\Entity\类别

此错误似乎是在执行下面列出的脚本期间发生的。这似乎有点奇怪,因为我正在尝试根据 findOneBy 教义函数获取的数据更新关系,该函数应该返回我的实体而不是数组

$Corka = $em->getRepository('AppBundle:Kategoria')->findOneByNazwa($row["nazwa"]);
$Ojciec = $em->getRepository('AppBundle:Kategoria')->findOneByNazwa($result[$row["rodzicId"]]["nazwa"]);
if(!$Corka) die ("Critical error: Corka: ".$row["nazwa"]."not found");
if(!$Ojciec) die ("Critical error: Ojciec: ".$result[$row["rodzicId"]]["nazwa"]."not found");
$Ojciec->addCorka($Corka);

我的 categoria.php:

/**
     * @ORM\ManyToOne(targetEntity="Kategoria", inversedBy="rodzic")
     * @ORM\JoinColumn(name="rodzic", referencedColumnName="id", nullable=true)
     **/
    private $corka;

    /**
     * @ORM\OneToMany(targetEntity="Kategoria", mappedBy="corka")
     **/
    private $rodzic;

/**
     * Add corka
     *
     * @param \AppBundle\Entity\Kategoria $corka
     * @return Kategoria
     */
    public function addCorka(\AppBundle\Entity\Kategoria $corka)
    {
        $this->corka[] = $corka;

        return $this;
    }

【问题讨论】:

    标签: symfony doctrine-orm doctrine


    【解决方案1】:

    在我的例子中,这个错误是由实体自引用关联映射中的错误注释引起的。

    可以在此处找到正确的映射注释: http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html

    就我而言,他们是:

    /**
         * @ORM\ManyToOne(targetEntity="Kategoria", inversedBy="corka")
         * @ORM\JoinColumn(name="rodzic", referencedColumnName="id", nullable=true)
         **/
        private $rodzic;
    
        /**
         * @ORM\ManyToMany(targetEntity="Ksiazka", mappedBy="kategoria")
         **/
          protected $ksiazka;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      • 1970-01-01
      • 2019-07-13
      • 2015-01-27
      • 1970-01-01
      相关资源
      最近更新 更多