forked from doctrine/orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DDC-1654] Add support for orphanRemoval on ManyToMany associations. …
…This only makes sense when ManyToMany is used as uni-directional OneToMany association with join table. The join column has a unique constraint on it to enforce this on the DB level, but we dont validate that this actually happens. Foreign Key constraints help prevent issues and notify developers early if they use it wrong.
- Loading branch information
Showing
10 changed files
with
147 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1654Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace Doctrine\Tests\ORM\Functional\Ticket; | ||
|
||
/** | ||
* @group DDC-1654 | ||
*/ | ||
class DDC1654Test extends \Doctrine\Tests\OrmFunctionalTestCase | ||
{ | ||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
$this->setUpEntitySchema(array( | ||
__NAMESPACE__ . '\\DDC1654Post', | ||
__NAMESPACE__ . '\\DDC1654Comment', | ||
)); | ||
} | ||
|
||
public function testManyToManyRemoveFromCollectionOrphanRemoval() | ||
{ | ||
$post = new DDC1654Post(); | ||
$post->comments[] = new DDC1654Comment(); | ||
$post->comments[] = new DDC1654Comment(); | ||
|
||
$this->_em->persist($post); | ||
$this->_em->flush(); | ||
|
||
$post->comments->remove(0); | ||
$post->comments->remove(1); | ||
|
||
$this->_em->flush(); | ||
$this->_em->clear(); | ||
|
||
$comments = $this->_em->getRepository(__NAMESPACE__ . '\\DDC1654Comment')->findAll(); | ||
$this->assertEquals(0, count($comments)); | ||
} | ||
|
||
public function testManyToManyRemoveElementFromCollectionOrphanRemoval() | ||
{ | ||
$post = new DDC1654Post(); | ||
$post->comments[] = new DDC1654Comment(); | ||
$post->comments[] = new DDC1654Comment(); | ||
|
||
$this->_em->persist($post); | ||
$this->_em->flush(); | ||
|
||
$post->comments->removeElement($post->comments[0]); | ||
$post->comments->removeElement($post->comments[1]); | ||
|
||
$this->_em->flush(); | ||
$this->_em->clear(); | ||
|
||
$comments = $this->_em->getRepository(__NAMESPACE__ . '\\DDC1654Comment')->findAll(); | ||
$this->assertEquals(0, count($comments)); | ||
} | ||
|
||
public function testManyToManyClearCollectionOrphanRemoval() | ||
{ | ||
$post = new DDC1654Post(); | ||
$post->comments[] = new DDC1654Comment(); | ||
$post->comments[] = new DDC1654Comment(); | ||
|
||
$this->_em->persist($post); | ||
$this->_em->flush(); | ||
|
||
$post->comments->clear(); | ||
|
||
$this->_em->flush(); | ||
$this->_em->clear(); | ||
|
||
$comments = $this->_em->getRepository(__NAMESPACE__ . '\\DDC1654Comment')->findAll(); | ||
$this->assertEquals(0, count($comments)); | ||
|
||
} | ||
} | ||
|
||
/** | ||
* @Entity | ||
*/ | ||
class DDC1654Post | ||
{ | ||
/** | ||
* @Id @Column(type="integer") @GeneratedValue | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @ManyToMany(targetEntity="DDC1654Comment", orphanRemoval=true, | ||
* cascade={"persist"}) | ||
*/ | ||
public $comments = array(); | ||
} | ||
|
||
/** | ||
* @Entity | ||
*/ | ||
class DDC1654Comment | ||
{ | ||
/** | ||
* @Id @Column(type="integer") @GeneratedValue | ||
*/ | ||
public $id; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters