doufuhao8085 2016-10-03 14: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 18: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 :)

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

报告相同问题?

悬赏问题

  • ¥20 三菱FX系列PLC上位机串口下指令置位M64和M65,这两条指令分别是什么呢?
  • ¥15 有关结冰模拟程序咨询
  • ¥15 ubuntu服务器配置dns域名后无法访问其他域名
  • ¥50 本人复制了一个关于股票指标的代码,但是运行失败,有没有人帮我解决一下
  • ¥50 用matlab和numeca做透平机械流体力学和热力学模拟 价格可议
  • ¥15 Unity3D WebView
  • ¥20 论文AlphaTensor复现(有偿)
  • ¥15 (有偿)在ANSYS中 .anf文件
  • ¥45 关于#芯片#的问题:组合逻辑电路设计
  • ¥15 基与机器学习和时间序列分析预测养老服务需求趋势