【问题标题】:Type error with ArrayCollection / OneToMany relationship in Symfony 3.4Symfony 3.4 中 ArrayCollection / OneToMany 关系的类型错误
【发布时间】:2018-11-19 19:37:30
【问题描述】:

在过去的几天里,我一直在尝试在 Symfony 3.4 中创建双向多对一-单对多关系 我有两个实体。一个是贡献,另一个是来源。一个贡献可以有多个来源。所以关系应该是

贡献 - 多对一 - 来源 - 一对多 - 贡献

但我在控制器中的$em→flush(); 期间不断收到以下错误:

类型错误:传递给 Doctrine\Common\Collections\ArrayCollection::__construct() 的参数 1 必须是数组类型,给定对象,在 /var/www/html/Edebate/vendor/doctrine/orm/lib 中调用/Doctrine/ORM/UnitOfWork.php 第 605 行

我的实体贡献中没有任何与数组集合相关的设置方法,正如我在此处的其他帖子中看到的那样:

Type error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given

Symfony-Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given

这里的注释没问题:

Doctrine OneToMany relationship error

任何帮助将不胜感激! :)

这是我的实体贡献

use Doctrine\Common\Collections\ArrayCollection;

//annotations

  abstract class Contribution
    {

    /**
    * @ORM\OneToMany(targetEntity="Shaker\DebateBundle\Entity\Source", mappedBy="parent")
    */
    protected $sources;

//Other attributes and methods


    public function __construct() {
       $this->sources = new ArrayCollection();
    }


    /**
     * Add source
     *
     * @param \Shaker\DebateBundle\Entity\Source $source
     *
     * @return Contribution
     */
    public function addSource(\Shaker\DebateBundle\Entity\Source $source)
    {
        $this->sources[] = $source;
        return $this;
    }

    /**
     * Remove source
     *
     * @param \Shaker\DebateBundle\Entity\Source $source
     */
    public function removeSource(\Shaker\DebateBundle\Entity\Source $source)
    {
        $this->sources->removeElement($source);
    }

    /**
     * Get sources
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getSources()
    {
        return $this->sources;
    }
}

这是在我的实体源中:

/**
* @ORM\ManyToOne(targetEntity="Shaker\DebateBundle\Entity\Contribution", inversedBy="sources")
*/
protected $parent;

   /**
     * Set parent
     *
     * @param \Shaker\DebateBundle\Entity\Contribution $parent
     *
     * @return Contribution
     */
    public function setParent(\Shaker\DebateBundle\Entity\Contribution $parent = null)
    {
        $this->parent = $parent;
        $parent->addSource($this);
        return $this;
    }

    /**
     * Get parent
     *
     * @return \Shaker\JRQBundle\Entity\Contribution
     */
    public function getParent()
    {
        return $this->parent;
    }

在我的控制器中,问题出现在flush:

        $formsourcebook->handleRequest($request);
        $contributionid=$formsourcebook->get('ContributionId')->getData();

        if ($formsourcebook->isValid()) {
            $topicargtarget=$this->getContribution($contributionid);
            $sourcebook->setUser($user);
            $sourcebook->setContribution($topicargtarget);
            $em->persist($sourcebook);
            $em->flush();
        }

【问题讨论】:

  • 有人知道这个错误是什么意思吗?我仍然被这个问题困扰,它似乎是一个非常普遍的错误日志。

标签: symfony doctrine-orm arraycollection


【解决方案1】:

我不太了解你的问题。但是,您是否尝试过在 Source 实体中使用此语法?

private $parent;
// ...

public function __construct() {
    $this->parent = new ArrayCollection();
    // or new \Doctrine\Common\Collections\ArrayCollection();
}

我认为你忘记了类中的构造函数。

【讨论】:

  • 是的,我有一个构造函数,但是在属性“Sources”的实体“Contribution”中,一个贡献可以有多个来源。它没有出现在我的第一篇文章中,所以我对其进行了编辑。
【解决方案2】:

我认为您在使用集合时“切换”了一些逻辑。我认为您的“添加”方法应该是这样的:

public function addSource(\Shaker\DebateBundle\Entity\Source $source)
{
    $this->sources[] = $source;
    $source->setParent($this);

    return $this;
}

在另一个实体中:

public function setParent(\Shaker\DebateBundle\Entity\Contribution $parent = null)
{
    $this->parent = $parent;

    return $this;
}

您的控制器 sn-p 中缺少变量以及表单字段定义,因此您在提交表单后不应该做太多工作。尝试直接映射尽可能多的字段(甚至通过自动猜测),即使它看起来很丑,但可以,但你可以稍后美化。只是我的两美分,延迟了几个月。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    相关资源
    最近更新 更多