[DDC-2382] Multiple relations between two same entities breaks cascade operations Created: 01/Apr/13  Updated: 14/Apr/13  Resolved: 14/Apr/13

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: 2.3.2
Fix Version/s: None
Security Level: All

Type: Bug Priority: Major
Reporter: Alex Assignee: Benjamin Eberlei
Resolution: Invalid Votes: 0
Labels: None


 Description   
/**
 * @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.


 Comments   
Comment by Benjamin Eberlei [ 14/Apr/13 ]

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

Generated at Thu Oct 02 00:45:47 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.