Skip to content

Loading…

DDC-2382: Multiple relations between two same entities breaks cascade operations #3092

Closed
doctrinebot opened this Issue · 2 comments

2 participants

@doctrinebot

Jira issue originally created by user drake_jc:

/****
 * @Entity
 */

class User
{
/****
 * @Id
 * @Column(type="integer")
 * @GeneratedValue(strategy="AUTO")
 */

protected $id;

/****
 * @OneToMany(targetEntity="Post", cascade={"remove"}, mappedBy="author")
 */

 protected $posts;

/****
 * @OneToOne(targetEntity="Post")
 */

 protected $lastPost;

public function setLastPost( $post )
{
    $this->lastPost = $post;
}

  // ...skipped...

}


/****
 * @Entity
 */

class Post
{
/****
 * @Id
 * @Column(type="integer")
 * @GeneratedValue(strategy="AUTO")
 */

 protected $id;

/****
 *
 * @ManyToOne(targetEntity="User", inversedBy="posts")
 * @JoinColumn(nullable=false)
 */

protected $author;

public function setAuthor( $author )
{
    $this->author = $author;
}

// ...skipped...
}

// Let's create user and post

$user = new User();
$em->persist($user);

$post = new Post();
$em->persist($post);

$post->setUser($user);
$user->setLastPost($post);

$em->flush();

// and now remove user

$user = $em->find('User', 1);
$em->remove($user);

$em->flush(); 
// "The DELETE statement conflicted with the REFERENCE 
// constraint...".
// Doctrine removes record from "User" table, ignoring associated records from "Post" table. If i remove "lastPost" relation 
//from User entity everything goes OK - at first Doctrine removes assiciated posts, then removes user itself.
@doctrinebot

Comment created by @beberlei:

Not an issue of Doctrine, you have to handle this case explicitly, because Doctrine has no way of doing so.

@doctrinebot

Issue was closed with resolution "Invalid"

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot closed this
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.