doufuhao8085 2016-10-03 06:00
浏览 31
已采纳

具有一对多的Doctrine ArrayCollection会引发错误

I get an error from Doctrine when persisting a one-to-many relationship. I've seen similar questions here, but the answers don't help, unfortunately.

Here is the error:

<b>Fatal error</b>:  Uncaught exception 'Doctrine\ORM\ORMInvalidArgumentException' with message 'Expected value of type &quot;Doctrine\Common\Collections\Collection|array&quot; for association field &quot;Entities\Scripts#$ScriptsVersions&quot;, got &quot;Entities\ScriptsVersions&quot; instead.' in H:\SERVER\htdocs\10pages\www\php\Vendors\doctrine\orm\lib\Doctrine\ORM\ORMInvalidArgumentException.php:206

Definition:

/**  
     *  @OneToMany(targetEntity="Scripts", mappedBy="ScriptsVersions",cascade={ "persist","remove"} ) 
    **/
    protected $ScriptsVersions;

Constructor:

public function __construct() {
$this->ScriptsVersions = new \Doctrine\Common\Collections\ArrayCollection();

    }

Setter:

public function setScriptsVersions(ScriptsVersions $ScriptsVersions){
        if(!$this->ScriptsVersions->contains($ScriptsVersions)){
            $this->ScriptsVersions[] = $ScriptsVersions;
            $ScriptsVersions->setScripts($this);
        }
    }

If I dump the class of $this->ScriptsVersions after assigning it, it says Doctrine\Common\Collections\Collection, but somehow UnityOfWork gets the object instead of the ArrayCollection...

  • 写回答

1条回答 默认 最新

  • duaeim2874 2016-10-04 10:15
    关注

    As I mentioned in the comment - the targetEntity in the code you pasted is not pointing at the correct entity class. Glad I could help :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部