简体   繁体   中英

error saving OneToMany and ManyToMany relationship with Doctrine 2

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!

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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