保存与 Doctrine 2 的 OneToMany 和 ManyToMany 关系时出错

发布于 2024-12-05 01:46:48 字数 1010 浏览 0 评论 0原文

我对“Doctrine2”有疑问。当尝试保存关系“ManyToMany”或“OneToOne”时,PHP 会留下异常错误!我留下错误以便您可以帮助我。

致命错误:未捕获的异常“InvalidArgumentException”,消息为“通过关系”实体\用户#特权“发现了一个新实体”,该实体未配置为级联持久操作实体:实体\特权@ 0000000012feb12000000000616126d4。显式或持久化新实体建立级联持久化关系上的操作。如果你无法找出问题的原因,可以通过实现实体'Entities\Privilege#__toString()'来获得线索。 “在C:\ Program Files \ EasyPHP-5.3.4.0 \ www \ mframework_2 \ phpinc \ Doctrine \ ORM \ UnitOfWork.php中第576行

我用来保持关系的代码是:

    $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();

在实体用户中我有这个:

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

我在多对多关系中也遇到同样的问题!

非常感谢!

I have a problem with "Doctrine2". When attempting to save a relationship "ManyToMany" or "OneToOne" PHP leave exception error! 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. 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. "in C: \ Program Files \ EasyPHP-5.3.4.0 \ www \ mframework_2 \ phpinc \ Doctrine \ ORM \ UnitOfWork.php on line 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:

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

I have the same problem whit ManyToMany relationships!

Thankyou very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔梦 2024-12-12 01:46:48

将cascade={"persist"} 添加到您的权限字段:

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

Add cascade={"persist"} to your privilege field:

/** @OneToOne(cascade={"persist"}, targetEntity="Privilege") */
protected $privilege;
人生戏 2024-12-12 01:46:48

执行以下操作之一:

1- 对用户和电子邮件对象使用持久性

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

2- 将级联添加到您的实体

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

Do one of these:

1- use persist for both user and email objects

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

or

2- add cascade to your entity

/** @OneToOne(targetEntity="Privilege", cascade={"persist"}) */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文