Skip to content

Commit

Permalink
#220 Set Doctrine/ORM cascade rules for Collection and CollectionRole…
Browse files Browse the repository at this point in the history
… relationships
  • Loading branch information
extracts committed Feb 23, 2022
1 parent 5d59e5b commit 85ec16a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
14 changes: 10 additions & 4 deletions library/Opus/Model2/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ class Collection extends AbstractModel
*/
private $parentId;

// TODO DOCTRINE Is `cascade={"persist"}` (i.e., storing the parent also stores its children) the desired behaviour?
// TODO DOCTRINE Test that if the associated parent gets stored/deleted, this collection will also get stored/deleted
/**
* @ORM\ManyToOne(targetEntity="Collection", inversedBy="children")
* @ORM\ManyToOne(targetEntity="Collection", inversedBy="children", cascade={"persist"})
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
*
* @Gedmo\TreeParent
Expand All @@ -94,7 +96,7 @@ class Collection extends AbstractModel
private $parent;

/**
* @ORM\OneToMany(targetEntity="Collection", mappedBy="parent")
* @ORM\OneToMany(targetEntity="Collection", mappedBy="parent", cascade={"persist", "remove"})
* @ORM\OrderBy({"left" = "ASC"})
*
* @var ORMCollection|self[]
Expand All @@ -108,9 +110,10 @@ class Collection extends AbstractModel
*/
private $roleId;

// TODO DOCTRINE Test that if the associated role gets deleted, this collection will also get deleted
/**
* @ORM\OneToOne(targetEntity="CollectionRole", inversedBy="rootCollection")
* @ORM\JoinColumn(name="role_id", referencedColumnName="id")
* @ORM\JoinColumn(name="role_id", referencedColumnName="id", onDelete="CASCADE")
*
* @var CollectionRole
*/
Expand Down Expand Up @@ -254,7 +257,10 @@ protected function removeChild($child)
}

$this->children->removeElement($child);
$child->setParent(null);

if ($child->getParent() === $this) {
$child->setParent(null);
}

return $this;
}
Expand Down
3 changes: 2 additions & 1 deletion library/Opus/Model2/CollectionRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ class CollectionRole extends AbstractModel
*/
private $language;

// TODO DOCTRINE Test that if this collection role gets stored, the associated root collection will also get stored
/**
* @ORM\OneToOne(targetEntity="Collection", mappedBy="role")
* @ORM\OneToOne(targetEntity="Collection", mappedBy="role", cascade={"persist"})
*
* @var Collection
*/
Expand Down
27 changes: 25 additions & 2 deletions tests/Opus/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ public function setUp()

$this->object = $this->roleFixture->addRootCollection();
$this->object->setTheme('dummy');
$this->object->store();

$this->roleFixture->store();
}

Expand Down Expand Up @@ -129,6 +127,31 @@ public function testDeleteNoChildren()
Collection::get($collectionId);
}

/**
* Test if delete also deletes any children.
*/
public function testDeleteChildren()
{
$collectionId = $this->object->getId();

$this->assertTrue(is_array($this->object->getChildren()));
$this->assertEquals(0, count($this->object->getChildren()), 'Root collection without children should return empty array.');

$child = $this->object->addLastChild();
$this->object->store();

$this->assertEquals(1, count($this->object->getChildren()), 'Root collection should have one child.');

$childId = $child->getId();
$this->object->delete();

$this->expectException(NotFoundException::class);
Collection::get($collectionId);

$this->expectException(NotFoundException::class);
Collection::get($childId);
}

/**
* Test if we can retrieve stored themes from the database.
*/
Expand Down

0 comments on commit 85ec16a

Please sign in to comment.