简体   繁体   English

使用Doctrine 2保存OneToMany和ManyToMany关系的错误

[英]error saving OneToMany and ManyToMany relationship with Doctrine 2

I have a problem with "Doctrine2". 我对“ Doctrine2”有疑问。 When attempting to save a relationship "ManyToMany" or "OneToOne" PHP leave exception error! 尝试保存关系“ ManyToMany”或“ OneToOne”时,PHP会抛出异常错误! I leave the error so that you can help me. 我留下错误,以便您可以帮助我。

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'A new entity WAS found Through the Relationship' Entities \\ User # privilege 'That Was not configured to cascade persist Operations for entity: Entities \\ Privilege @ 0000000012feb12000000000616126d4. 致命错误:未捕获的异常'InvalidArgumentException'消息为'通过关系找到了一个新实体'实体\\用户#特权'未配置为级联持久存储实体的操作:实体\\特权@ 0000000012feb12000000000616126d4。 Explicitly or persist the new entity set up cascading persist Operations on the relationship. 显式或持久化新实体设置级联持久化关系上的操作。 If you can not find out Which Causes the problem by implementing entity 'Entities \\ Privilege # __toString ()' to get a clue. 如果您无法通过实现实体'Entities \\ Privilege#__toString()'来找出导致问题的原因,则可以获取线索。 "in C: \\ Program Files \\ EasyPHP-5.3.4.0 \\ www \\ mframework_2 \\ phpinc \\ Doctrine \\ ORM \\ UnitOfWork.php on line 576 “在C:\\ Program Files \\ EasyPHP-5.3.4.0 \\ www \\ mframework_2 \\ phpinc \\ Doctrine \\ ORM \\ UnitOfWork.php上的第576行

The code I use to keep the relationship is: 我用来保持这种关系的代码是:

$user = new \Entities\User();
            $user->setActive(true);
            $user->setUsername('xxx');
            $user->setPassword('xxx');

    $email = new \Entities\Email();
            $email->setEmail(xxx');
            $email->setType('xxx');

    $user->addEmail($email);

    $this->em->persist($user);
            $this->em->flush();

In the Entitie User I have this: 在Entitie用户中,我有以下内容:

/** @OneToOne(targetEntity="Privilege") */
    protected $privilege;

I have the same problem whit ManyToMany relationships! 我与ManyToMany关系有同样的问题!

Thankyou very much! 非常感谢你!

Add cascade={"persist"} to your privilege field: 在您的特权字段中添加cascade = {“ persist”}:

/** @OneToOne(cascade={"persist"}, targetEntity="Privilege") */
protected $privilege;

Do one of these: 执行以下操作之一:

1- use persist for both user and email objects 1-对用户和电子邮件对象都使用持久化

$this->em->persist($user);
$this->em->persist($email);
$this->em->flush();

or 要么

2- add cascade to your entity 2-将级联添加到您的实体

/** @OneToOne(targetEntity="Privilege", cascade={"persist"}) */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM