Skip to content

Commit

Permalink
Avoid unnecessary changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mpdude committed Jun 21, 2023
1 parent ee0b3f2 commit ba089e5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
19 changes: 19 additions & 0 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Doctrine\ORM\Event\PreRemoveEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\Exception\UnexpectedAssociationValue;
use Doctrine\ORM\Id\AssignedGenerator;
use Doctrine\ORM\Internal\CommitOrderCalculator;
use Doctrine\ORM\Internal\HydrationCompleteHandler;
Expand Down Expand Up @@ -955,6 +956,15 @@ private function computeAssociationChanges(array $assoc, $value): void

$state = $this->getEntityState($entry, self::STATE_NEW);

if (! ($entry instanceof $assoc['targetEntity'])) {
throw UnexpectedAssociationValue::create(
$assoc['sourceEntity'],
$assoc['fieldName'],
get_debug_type($entry),
$assoc['targetEntity']
);
}

switch ($state) {
case self::STATE_NEW:
if (! $assoc['isCascadePersist']) {
Expand Down Expand Up @@ -990,6 +1000,15 @@ private function computeAssociationChanges(array $assoc, $value): void

$this->pendingCollectionElementRemovals[$coid][$key] = true;
break;

case self::STATE_DETACHED:
// Can actually not happen right now as we assume STATE_NEW,
// so the exception will be raised from the DBAL layer (constraint violation).
throw ORMInvalidArgumentException::detachedEntityFoundThroughRelationship($assoc, $entry);

default:
// MANAGED associated entities are already taken into account
// during changeset calculation anyway, since they are in the identity map.
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ public function testRemoveGroupWithUser(): void
{
$user = $this->addCmsUserGblancoWithGroups(5);

$anotherUser = new CmsUser();
$anotherUser = new CmsUser();
$anotherUser->username = 'joe_doe';
$anotherUser->name = 'Joe Doe';
$anotherUser->status = 'QA Engineer';
$anotherUser->name = 'Joe Doe';
$anotherUser->status = 'QA Engineer';

foreach ($user->getGroups() as $group) {
$anotherUser->addGroup($group);
Expand Down

0 comments on commit ba089e5

Please sign in to comment.