Skip to content

Commit

Permalink
Remove EntityManager::getPartialReference()
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Oct 11, 2023
1 parent bc0f666 commit 9162bea
Show file tree
Hide file tree
Showing 10 changed files with 1 addition and 116 deletions.
1 change: 1 addition & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- `Doctrine\ORM\Query\AST\PartialObjectExpression`is removed.
- `Doctrine\ORM\Query\SqlWalker::HINT_PARTIAL` and
`Doctrine\ORM\Query::HINT_FORCE_PARTIAL_LOAD` are removed.
- `Doctrine\ORM\EntityManager*::getPartialReference()` is removed.

## BC BREAK: `Doctrine\ORM\Persister\Entity\EntityPersister::executeInserts()` return type changed to `void`

Expand Down
5 changes: 0 additions & 5 deletions lib/Doctrine/ORM/Decorator/EntityManagerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ public function getReference(string $entityName, mixed $id): object|null
return $this->wrapped->getReference($entityName, $id);
}

public function getPartialReference(string $entityName, mixed $identifier): object|null
{
return $this->wrapped->getPartialReference($entityName, $identifier);
}

public function close(): void
{
$this->wrapped->close();
Expand Down
25 changes: 0 additions & 25 deletions lib/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,31 +400,6 @@ public function getReference(string $entityName, mixed $id): object|null
return $entity;
}

public function getPartialReference(string $entityName, mixed $identifier): object|null
{
$class = $this->metadataFactory->getMetadataFor(ltrim($entityName, '\\'));

$entity = $this->unitOfWork->tryGetById($identifier, $class->rootEntityName);

// Check identity map first, if its already in there just return it.
if ($entity !== false) {
return $entity instanceof $class->name ? $entity : null;
}

if (! is_array($identifier)) {
$identifier = [$class->identifier[0] => $identifier];
}

$entity = $class->newInstance();

$class->setIdentifierValues($entity, $identifier);

$this->unitOfWork->registerManaged($entity, $identifier, []);
$this->unitOfWork->markReadOnly($entity);

return $entity;
}

/**
* Clears the EntityManager. All entities that are currently managed
* by this EntityManager become detached.
Expand Down
25 changes: 0 additions & 25 deletions lib/Doctrine/ORM/EntityManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,31 +163,6 @@ public function refresh(object $object, LockMode|int|null $lockMode = null): voi
*/
public function getReference(string $entityName, mixed $id): object|null;

/**
* Gets a partial reference to the entity identified by the given type and identifier
* without actually loading it, if the entity is not yet loaded.
*
* The returned reference may be a partial object if the entity is not yet loaded/managed.
* If it is a partial object it will not initialize the rest of the entity state on access.
* Thus you can only ever safely access the identifier of an entity obtained through
* this method.
*
* The use-cases for partial references involve maintaining bidirectional associations
* without loading one side of the association or to update an entity without loading it.
* Note, however, that in the latter case the original (persistent) entity data will
* never be visible to the application (especially not event listeners) as it will
* never be loaded in the first place.
*
* @param string $entityName The name of the entity type.
* @param mixed $identifier The entity identifier.
* @psalm-param class-string<T> $entityName
*
* @psalm-return T|null
*
* @template T of object
*/
public function getPartialReference(string $entityName, mixed $identifier): object|null;

/**
* Closes the EntityManager. All entities that are currently managed
* by this EntityManager become detached. The EntityManager may no longer
Expand Down
2 changes: 0 additions & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@
<code>$entity</code>
<code>$entity</code>
<code>$entity</code>
<code>$entity</code>
<code><![CDATA[$entity instanceof $class->name ? $entity : null]]></code>
<code><![CDATA[$entity instanceof $class->name ? $entity : null]]></code>
<code><![CDATA[$persister->load($sortedId, null, null, [], $lockMode)]]></code>
<code><![CDATA[$persister->loadById($sortedId)]]></code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,6 @@ public function getReference(string $entityName, mixed $id): object|null
return $this->realEntityManager->getReference($entityName, $id);
}

public function getPartialReference(string $entityName, mixed $identifier): object|null
{
return $this->realEntityManager->getPartialReference($entityName, $identifier);
}

public function close(): void
{
$this->realEntityManager->close();
Expand Down
8 changes: 0 additions & 8 deletions tests/Doctrine/Tests/ORM/EntityManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,6 @@ public function testCreateQueryDqlIsOptional(): void
self::assertInstanceOf(Query::class, $this->entityManager->createQuery());
}

public function testGetPartialReference(): void
{
$user = $this->entityManager->getPartialReference(CmsUser::class, 42);
self::assertTrue($this->entityManager->contains($user));
self::assertEquals(42, $user->id);
self::assertNull($user->getName());
}

public function testCreateQuery(): void
{
$q = $this->entityManager->createQuery('SELECT 1');
Expand Down
23 changes: 0 additions & 23 deletions tests/Doctrine/Tests/ORM/Functional/BasicFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -916,29 +916,6 @@ public function testOneToOneOrphanRemoval(): void
self::assertEquals(1, $this->_em->getConnection()->fetchOne('select count(*) from cms_addresses'));
}

public function testGetPartialReferenceToUpdateObjectWithoutLoadingIt(): void
{
$user = new CmsUser();
$user->username = 'beberlei';
$user->name = 'Benjamin E.';
$user->status = 'active';
$this->_em->persist($user);
$this->_em->flush();
$userId = $user->id;
$this->_em->clear();

$user = $this->_em->getPartialReference(CmsUser::class, $userId);
self::assertTrue($this->_em->contains($user));
self::assertNull($user->getName());
self::assertEquals($userId, $user->id);

$user->name = 'Stephan';
$this->_em->flush();
$this->_em->clear();

self::assertEquals('Benjamin E.', $this->_em->find($user::class, $userId)->name);
}

#[Group('DDC-952')]
public function testManyToOneFetchModeQuery(): void
{
Expand Down
22 changes: 0 additions & 22 deletions tests/Doctrine/Tests/ORM/Functional/DefaultValuesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,6 @@ public function testSimpleDetachMerge(): void
self::assertEquals($userId, $a2->getUser()->getId());
self::assertEquals('Poweruser', $a2->getUser()->type);
}

#[Group('DDC-1386')]
public function testGetPartialReferenceWithDefaultValueNotEvaluatedInFlush(): void
{
$user = new DefaultValueUser();
$user->name = 'romanb';
$user->type = 'Normaluser';

$this->_em->persist($user);
$this->_em->flush();
$this->_em->clear();

$user = $this->_em->getPartialReference(DefaultValueUser::class, $user->id);
self::assertTrue($this->_em->getUnitOfWork()->isReadOnly($user));

$this->_em->flush();
$this->_em->clear();

$user = $this->_em->find(DefaultValueUser::class, $user->id);

self::assertEquals('Normaluser', $user->type);
}
}


Expand Down
1 change: 0 additions & 1 deletion tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1041Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,5 @@ public function testGrabWrongSubtypeReturnsNull(): void

self::assertNull($this->_em->find(CompanyFlexContract::class, $id));
self::assertNull($this->_em->getReference(CompanyFlexContract::class, $id));
self::assertNull($this->_em->getPartialReference(CompanyFlexContract::class, $id));
}
}

0 comments on commit 9162bea

Please sign in to comment.