error saving OneToMany and ManyToMany relationship with Doctrine 2

五迷三道 提交于 2019-12-08 07:07:11

问题


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!


回答1:


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

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



回答2:


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"}) */


来源:https://stackoverflow.com/questions/7467982/error-saving-onetomany-and-manytomany-relationship-with-doctrine-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!