diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index ad880eb64..f4bc45a40 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -661,20 +661,21 @@ private function addCollectionRelation(BaseCollectionRelation $relation) $paramBuilder->setTypeHint($typeHint); $removerNodeBuilder->addParam($paramBuilder->getNode()); - // add if check to see if item actually exists - //if ($this->avatars->contains($avatar)) - $ifContainsStmt = new Node\Stmt\If_($containsMethodCallNode); - $removerNodeBuilder->addStmt($ifContainsStmt); - - // call removeElement - $ifContainsStmt->stmts[] = BuilderHelpers::normalizeStmt(new Node\Expr\MethodCall( + // $this->avatars->removeElement($avatar) + $removeElementCall = new Node\Expr\MethodCall( new Node\Expr\PropertyFetch(new Node\Expr\Variable('this'), $relation->getPropertyName()), 'removeElement', [new Node\Expr\Variable($argName)] - )); + ); // set the owning side of the relationship - if (!$relation->isOwning()) { + if ($relation->isOwning()) { + // $this->avatars->removeElement($avatar); + $removerNodeBuilder->addStmt(BuilderHelpers::normalizeStmt($removeElementCall)); + } else { + //if ($this->avatars->removeElement($avatar)) + $ifRemoveElementStmt = new Node\Stmt\If_($removeElementCall); + $removerNodeBuilder->addStmt($ifRemoveElementStmt); if ($relation instanceof RelationOneToMany) { // OneToMany: $student->setCourse(null); /* @@ -684,7 +685,7 @@ private function addCollectionRelation(BaseCollectionRelation $relation) * } */ - $ifContainsStmt->stmts[] = $this->createSingleLineCommentNode( + $ifRemoveElementStmt->stmts[] = $this->createSingleLineCommentNode( 'set the owning side to null (unless already changed)', self::CONTEXT_CLASS_METHOD ); @@ -707,14 +708,16 @@ private function addCollectionRelation(BaseCollectionRelation $relation) )), ]; - $ifContainsStmt->stmts[] = $ifNode; + $ifRemoveElementStmt->stmts[] = $ifNode; } elseif ($relation instanceof RelationManyToMany) { - // ManyToMany: $student->removeCourse($this); - $ifContainsStmt->stmts[] = new Node\Stmt\Expression(new Node\Expr\MethodCall( - new Node\Expr\Variable($argName), - $relation->getTargetRemoverMethodName(), - [new Node\Expr\Variable('this')] - )); + // $student->removeCourse($this); + $ifRemoveElementStmt->stmts[] = new Node\Stmt\Expression( + new Node\Expr\MethodCall( + new Node\Expr\Variable($argName), + $relation->getTargetRemoverMethodName(), + [new Node\Expr\Variable('this')] + ) + ); } else { throw new \Exception('Unknown relation type'); } diff --git a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/Client.php b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/Client.php index 308b93217..144265697 100644 --- a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/Client.php +++ b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/Client.php @@ -79,9 +79,7 @@ public function addTag(Tag $tag): self public function removeTag(Tag $tag): self { - if ($this->tags->contains($tag)) { - $this->tags->removeElement($tag); - } + $this->tags->removeElement($tag); return $this; } diff --git a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/User.php b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/User.php index 3524c472d..9fd6a1d81 100644 --- a/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/User.php +++ b/tests/Doctrine/fixtures/expected_no_overwrite/src/Entity/User.php @@ -75,8 +75,7 @@ public function addAvatar(UserAvatar $avatar): self public function removeAvatar(UserAvatar $avatar): self { - if ($this->avatars->contains($avatar)) { - $this->avatars->removeElement($avatar); + if ($this->avatars->removeElement($avatar)) { // set the owning side to null (unless already changed) if ($avatar->getUser() === $this) { $avatar->setUser(null); @@ -110,9 +109,7 @@ public function addTag(Tag $tag): self public function removeTag(Tag $tag): self { - if ($this->tags->contains($tag)) { - $this->tags->removeElement($tag); - } + $this->tags->removeElement($tag); return $this; } diff --git a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/Client.php b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/Client.php index 308b93217..144265697 100644 --- a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/Client.php +++ b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/Client.php @@ -79,9 +79,7 @@ public function addTag(Tag $tag): self public function removeTag(Tag $tag): self { - if ($this->tags->contains($tag)) { - $this->tags->removeElement($tag); - } + $this->tags->removeElement($tag); return $this; } diff --git a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/User.php b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/User.php index 2e42b65a3..91fdbcee3 100644 --- a/tests/Doctrine/fixtures/expected_overwrite/src/Entity/User.php +++ b/tests/Doctrine/fixtures/expected_overwrite/src/Entity/User.php @@ -82,8 +82,7 @@ public function addAvatar(UserAvatar $avatar): self public function removeAvatar(UserAvatar $avatar): self { - if ($this->avatars->contains($avatar)) { - $this->avatars->removeElement($avatar); + if ($this->avatars->removeElement($avatar)) { // set the owning side to null (unless already changed) if ($avatar->getUser() === $this) { $avatar->setUser(null); @@ -117,9 +116,7 @@ public function addTag(Tag $tag): self public function removeTag(Tag $tag): self { - if ($this->tags->contains($tag)) { - $this->tags->removeElement($tag); - } + $this->tags->removeElement($tag); return $this; } diff --git a/tests/Doctrine/fixtures/expected_xml/src/Entity/UserXml.php b/tests/Doctrine/fixtures/expected_xml/src/Entity/UserXml.php index b5df783be..9c0a3a700 100644 --- a/tests/Doctrine/fixtures/expected_xml/src/Entity/UserXml.php +++ b/tests/Doctrine/fixtures/expected_xml/src/Entity/UserXml.php @@ -56,8 +56,7 @@ public function addAvatar(UserAvatar $avatar): self public function removeAvatar(UserAvatar $avatar): self { - if ($this->avatars->contains($avatar)) { - $this->avatars->removeElement($avatar); + if ($this->avatars->removeElement($avatar)) { // set the owning side to null (unless already changed) if ($avatar->getUser() === $this) { $avatar->setUser(null); diff --git a/tests/Util/fixtures/add_many_to_many_relation/User_simple_inverse.php b/tests/Util/fixtures/add_many_to_many_relation/User_simple_inverse.php index 5662d25a9..fa2bc65d8 100644 --- a/tests/Util/fixtures/add_many_to_many_relation/User_simple_inverse.php +++ b/tests/Util/fixtures/add_many_to_many_relation/User_simple_inverse.php @@ -53,8 +53,7 @@ public function addRecipe(Recipe $recipe): self public function removeRecipe(Recipe $recipe): self { - if ($this->recipes->contains($recipe)) { - $this->recipes->removeElement($recipe); + if ($this->recipes->removeElement($recipe)) { $recipe->removeFood($this); } diff --git a/tests/Util/fixtures/add_many_to_many_relation/User_simple_no_inverse.php b/tests/Util/fixtures/add_many_to_many_relation/User_simple_no_inverse.php index 8421cd682..2d1e7288d 100644 --- a/tests/Util/fixtures/add_many_to_many_relation/User_simple_no_inverse.php +++ b/tests/Util/fixtures/add_many_to_many_relation/User_simple_no_inverse.php @@ -52,9 +52,7 @@ public function addRecipe(Recipe $recipe): self public function removeRecipe(Recipe $recipe): self { - if ($this->recipes->contains($recipe)) { - $this->recipes->removeElement($recipe); - } + $this->recipes->removeElement($recipe); return $this; } diff --git a/tests/Util/fixtures/add_many_to_many_relation/User_simple_owning.php b/tests/Util/fixtures/add_many_to_many_relation/User_simple_owning.php index d822debe5..e1b079156 100644 --- a/tests/Util/fixtures/add_many_to_many_relation/User_simple_owning.php +++ b/tests/Util/fixtures/add_many_to_many_relation/User_simple_owning.php @@ -52,9 +52,7 @@ public function addRecipe(Recipe $recipe): self public function removeRecipe(Recipe $recipe): self { - if ($this->recipes->contains($recipe)) { - $this->recipes->removeElement($recipe); - } + $this->recipes->removeElement($recipe); return $this; } diff --git a/tests/Util/fixtures/add_one_to_many_relation/User_simple.php b/tests/Util/fixtures/add_one_to_many_relation/User_simple.php index c078da61c..19b79ca62 100644 --- a/tests/Util/fixtures/add_one_to_many_relation/User_simple.php +++ b/tests/Util/fixtures/add_one_to_many_relation/User_simple.php @@ -53,8 +53,7 @@ public function addAvatarPhoto(UserAvatarPhoto $avatarPhoto): self public function removeAvatarPhoto(UserAvatarPhoto $avatarPhoto): self { - if ($this->avatarPhotos->contains($avatarPhoto)) { - $this->avatarPhotos->removeElement($avatarPhoto); + if ($this->avatarPhotos->removeElement($avatarPhoto)) { // set the owning side to null (unless already changed) if ($avatarPhoto->getUser() === $this) { $avatarPhoto->setUser(null); diff --git a/tests/Util/fixtures/add_one_to_many_relation/User_simple_orphan_removal.php b/tests/Util/fixtures/add_one_to_many_relation/User_simple_orphan_removal.php index b71daa37b..4f92ff0fd 100644 --- a/tests/Util/fixtures/add_one_to_many_relation/User_simple_orphan_removal.php +++ b/tests/Util/fixtures/add_one_to_many_relation/User_simple_orphan_removal.php @@ -53,8 +53,7 @@ public function addAvatarPhoto(UserAvatarPhoto $avatarPhoto): self public function removeAvatarPhoto(UserAvatarPhoto $avatarPhoto): self { - if ($this->avatarPhotos->contains($avatarPhoto)) { - $this->avatarPhotos->removeElement($avatarPhoto); + if ($this->avatarPhotos->removeElement($avatarPhoto)) { // set the owning side to null (unless already changed) if ($avatarPhoto->getUser() === $this) { $avatarPhoto->setUser(null); diff --git a/tests/Util/fixtures/add_one_to_many_relation/User_with_use_statements.php b/tests/Util/fixtures/add_one_to_many_relation/User_with_use_statements.php index 273e9c703..df6e9ed06 100644 --- a/tests/Util/fixtures/add_one_to_many_relation/User_with_use_statements.php +++ b/tests/Util/fixtures/add_one_to_many_relation/User_with_use_statements.php @@ -55,8 +55,7 @@ public function addAvatarPhoto(UserAvatarPhoto $avatarPhoto): self public function removeAvatarPhoto(UserAvatarPhoto $avatarPhoto): self { - if ($this->avatarPhotos->contains($avatarPhoto)) { - $this->avatarPhotos->removeElement($avatarPhoto); + if ($this->avatarPhotos->removeElement($avatarPhoto)) { // set the owning side to null (unless already changed) if ($avatarPhoto->getUser() === $this) { $avatarPhoto->setUser(null);