Skip to content

Instantly share code, notes, and snippets.

@doctrinebot
Created December 13, 2015 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doctrinebot/3c278decf006f6cc62ba to your computer and use it in GitHub Desktop.
Save doctrinebot/3c278decf006f6cc62ba to your computer and use it in GitHub Desktop.
Attachments to Doctrine Jira Issue DDC-735 - https://github.com/doctrine/doctrine2/issues/5248
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Common\Collections\ArrayCollection;
require_once __DIR__ . '/../../../TestInit.php';
class DDC735Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
protected function setUp()
{
parent::setUp();
try {
$this->_schemaTool->createSchema(array(
$this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC735Product'),
$this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC735Review')
));
} catch(\Exception $e) {
}
}
public function testRemoveElement_AppliesOrphanRemoval()
{
// Create a product and its first review
$product = new DDC735Product;
$review = new DDC735Review($product);
// Persist and flush
$this->_em->persist($product);
$this->_em->flush();
// Now you see it
$this->assertEquals(1, count($product->getReviews()));
// Remove the review
$product->removeReview($review);
$this->_em->flush();
// Now you don't
$this->assertEquals(0, count($product->getReviews()), 'count($reviews) should be 0 after removing its only Review');
// Refresh
$this->_em->refresh($product);
// It should still be 0
$this->assertEquals(0, count($product->getReviews()), 'count($reviews) should still be 0 after the refresh');
}
}
/**
* @Entity
*/
class DDC735Product
{
/**
* @Id @Column(type="integer") @GeneratedValue
*/
protected $id;
/**
* @OneToMany(
* targetEntity="DDC735Review",
* mappedBy="product",
* cascade={"persist"},
* orphanRemoval=true
* )
*/
protected $reviews;
public function __construct()
{
$this->reviews = new ArrayCollection;
}
public function getReviews()
{
return $this->reviews;
}
public function addReview(DDC735Review $review)
{
$this->reviews->add($review);
}
public function removeReview(DDC735Review $review)
{
$this->reviews->removeElement($review);
}
}
/**
* @Entity
*/
class DDC735Review
{
/**
* @Id @Column(type="integer") @GeneratedValue
*/
protected $id;
/**
* @ManyToOne(targetEntity="DDC735Product", inversedBy="reviews")
*/
protected $product;
public function __construct(DDC735Product $product)
{
$this->product = $product;
$product->addReview($this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment