Skip to content

Commit 4b42fc3

Browse files
committed
Not setting the owning side (from inverse) on OneToOne
Now consistent with other types
1 parent 13d4bf8 commit 4b42fc3

File tree

5 files changed

+8
-42
lines changed

5 files changed

+8
-42
lines changed

src/Util/ClassSourceManipulator.php

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,13 @@ private function addSingularRelation(BaseRelation $relation)
293293
$relation instanceof RelationOneToOne ? $relation->isNullable() : true
294294
);
295295

296-
// set the inverse side of the relation for OneToOne
297-
// we don't do this for ManyToOne, because setting the inverse collection
298-
// side could be dangerous, as it could cause many records to hydrate
299-
if ($relation instanceof RelationOneToOne) {
300-
// only set if this is the inverse side (i.e. we're setting the owning)
301-
// of if the inverse side *is* being mapped
302-
if (!$relation->isOwning() || $relation->getMapInverseRelation()) {
303-
$setterNodeBuilder->addStmt($this->createBlankLineNode(self::CONTEXT_CLASS_METHOD));
296+
// set the *owning* side of the relation
297+
// OneToOne is the only "singular" relation type that
298+
// may be the inverse side
299+
if ($relation instanceof RelationOneToOne && !$relation->isOwning()) {
300+
$setterNodeBuilder->addStmt($this->createBlankLineNode(self::CONTEXT_CLASS_METHOD));
304301

305-
$this->addNodesToSetOtherSideOfOneToOne($relation, $setterNodeBuilder);
306-
}
302+
$this->addNodesToSetOtherSideOfOneToOne($relation, $setterNodeBuilder);
307303
}
308304

309305
$this->addMethod($setterNodeBuilder->getNode());
@@ -894,10 +890,7 @@ private function addNodesToSetOtherSideOfOneToOne(RelationOneToOne $relation, Bu
894890
{
895891
if (!$relation->isNullable()) {
896892
$setterNodeBuilder->addStmt($this->createSingleLineCommentNode(
897-
sprintf(
898-
'set the %s side of the relation if necessary',
899-
$relation->isOwning() ? 'inverse' : 'owning'
900-
),
893+
'set the owning side of the relation if necessary',
901894
self::CONTEXT_CLASS_METHOD
902895
));
903896

@@ -925,10 +918,7 @@ private function addNodesToSetOtherSideOfOneToOne(RelationOneToOne $relation, Bu
925918

926919
// at this point, we know the relation is nullable
927920
$setterNodeBuilder->addStmt($this->createSingleLineCommentNode(
928-
sprintf(
929-
'set (or unset) the %s side of the relation if necessary',
930-
$relation->isOwning() ? 'inverse' : 'owning'
931-
),
921+
'set (or unset) the owning side of the relation if necessary',
932922
self::CONTEXT_CLASS_METHOD
933923
));
934924

tests/Util/fixtures/add_one_to_one_relation/User_simple_owning.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ public function getUserProfile(): ?UserProfile
3434
public function setUserProfile(?UserProfile $userProfile)
3535
{
3636
$this->userProfile = $userProfile;
37-
38-
// set (or unset) the inverse side of the relation if necessary
39-
$newUser = $userProfile === null ? null : $this;
40-
if ($newUser !== $userProfile->getUser()) {
41-
$userProfile->setUser($newUser);
42-
}
4337
}
4438

4539
// add your own fields

tests/Util/fixtures/add_one_to_one_relation/User_simple_self.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ public function getEmbeddedUser(): ?self
3434
public function setEmbeddedUser(?self $embeddedUser)
3535
{
3636
$this->embeddedUser = $embeddedUser;
37-
38-
// set (or unset) the inverse side of the relation if necessary
39-
$newUser = $embeddedUser === null ? null : $this;
40-
if ($newUser !== $embeddedUser->getUser()) {
41-
$embeddedUser->setUser($newUser);
42-
}
4337
}
4438

4539
// add your own fields

tests/Util/fixtures/add_one_to_one_relation/User_with_use_statements_avoid_duplicate_use.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ public function getUserProfile(): ?\App\OtherEntity\UserProfile
3737
public function setUserProfile(?\App\OtherEntity\UserProfile $userProfile)
3838
{
3939
$this->userProfile = $userProfile;
40-
41-
// set (or unset) the inverse side of the relation if necessary
42-
$newUser = $userProfile === null ? null : $this;
43-
if ($newUser !== $userProfile->getUser()) {
44-
$userProfile->setUser($newUser);
45-
}
4640
}
4741

4842
// add your own fields

tests/Util/fixtures/add_one_to_one_relation/User_with_use_statements_avoid_duplicate_use_alias.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ public function getCategory(): ?\App\OtherEntity\Category
3737
public function setCategory(?\App\OtherEntity\Category $category)
3838
{
3939
$this->category = $category;
40-
41-
// set (or unset) the inverse side of the relation if necessary
42-
$newUser = $category === null ? null : $this;
43-
if ($newUser !== $category->getUser()) {
44-
$category->setUser($newUser);
45-
}
4640
}
4741

4842
// add your own fields

0 commit comments

Comments
 (0)