Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
/*
Expand All @@ -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
);
Expand All @@ -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');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions tests/Doctrine/fixtures/expected_xml/src/Entity/UserXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions tests/Util/fixtures/add_one_to_many_relation/User_simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down