8000 Migrate to the object API of AssociationMapping by greg0ire · Pull Request #10723 · doctrine/orm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Migrate to the object API of AssociationMapping #10723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from 8000
May 25, 2023
5 changes: 3 additions & 2 deletions lib/Doctrine/ORM/Cache/DefaultCacheFactory.php
629A
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ public function buildCachedCollectionPersister(
CollectionPersister $persister,
AssociationMapping $mapping,
): CachedCollectionPersister {
$usage = $mapping['cache']['usage'];
$region = $this->getRegion($mapping['cache']);
assert(isset($mapping->cache));
$usage = $mapping->cache['usage'];
$region = $this->getRegion($mapping->cache);

if ($usage === ClassMetadata::CACHE_USAGE_READ_ONLY) {
return new ReadOnlyCachedCollectionPersister($persister, $region, $em, $mapping);
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/Cache/DefaultCollectionHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function buildCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key
public function loadCacheEntry(ClassMetadata $metadata, CollectionCacheKey $key, CollectionCacheEntry $entry, PersistentCollection $collection): array|null
{
$assoc = $metadata->associationMappings[$key->association];
$targetPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
$targetPersister = $this->uow->getEntityPersister($assoc->targetEntity);
assert($targetPersister instanceof CachedPersister);
$targetRegion = $targetPersister->getCacheRegion();
$list = [];
Expand Down
29 changes: 16 additions & 13 deletions lib/Doctrine/ORM/Cache/DefaultEntityHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public function buildCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, ob
continue;
}

if (! isset($assoc['cache'])) {
$targetClassMetadata = $this->em->getClassMetadata($assoc['targetEntity']);
if (! isset($assoc->cache)) {
$targetClassMetadata = $this->em->getClassMetadata($assoc->targetEntity);
$owningAssociation = $this->em->getMetadataFactory()->getOwningSide($assoc);
$associationIds = $this->identifierFlattener->flattenIdentifier(
$targetClassMetadata,
Expand All @@ -75,17 +75,19 @@ public function buildCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, ob

foreach ($associationIds as $fieldName => $fieldValue) {
if (isset($targetClassMetadata->fieldMappings[$fieldName])) {
assert($owningAssociation->isToOneOwningSide());
$fieldMapping = $targetClassMetadata->fieldMappings[$fieldName];

$data[$owningAssociation['targetToSourceKeyColumns'][$fieldMapping->columnName]] = $fieldValue;
$data[$owningAssociation->targetToSourceKeyColumns[$fieldMapping->columnName]] = $fieldValue;

continue;
}

$targetAssoc = $targetClassMetadata->associationMappings[$fieldName];

foreach ($assoc['targetToSourceKeyColumns'] as $referencedColumn => $localColumn) {
if (isset($targetAssoc['sourceToTargetKeyColumns'][$referencedColumn])) {
assert($assoc->isToOneOwningSide());
foreach ($assoc->targetToSourceKeyColumns as $referencedColumn => $localColumn) {
if (isset($targetAssoc->sourceToTargetKeyColumns[$referencedColumn])) {
$data[$localColumn] = $fieldValue;
}
}
Expand All @@ -94,7 +96,7 @@ public function buildCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, ob
continue;
}

if (! isset($assoc['id'])) {
if (! isset($assoc->id)) {
$targetClass = ClassUtils::getClass($data[$name]);
$targetId = $this->uow->getEntityIdentifier($data[$name]);
$data[$name] = new AssociationCacheEntry($targetClass, $targetId);
Expand All @@ -110,13 +112,14 @@ public function buildCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, ob
// @TODO - fix it !
// handle UnitOfWork#createEntity hash generation
if (! is_array($targetId)) {
$data[reset($assoc['joinColumnFieldNames'])] = $targetId;
assert($assoc->isToOneOwningSide());
$data[reset($assoc->joinColumnFieldNames)] = $targetId;

$targetEntity = $this->em->getClassMetadata($assoc['targetEntity']);
$targetEntity = $this->em->getClassMetadata($assoc->targetEntity);
$targetId = [$targetEntity->identifier[0] => $targetId];
}

$data[$name] = new AssociationCacheEntry($assoc['targetEntity'], $targetId);
$data[$name] = new AssociationCacheEntry($assoc->targetEntity, $targetId);
}

return new EntityCacheEntry($metadata->name, $data);
Expand All @@ -133,23 +136,23 @@ public function loadCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, Ent
}

foreach ($metadata->associationMappings as $name => $assoc) {
if (! isset($assoc['cache']) || ! isset($data[$name])) {
if (! isset($assoc->cache) || ! isset($data[$name])) {
continue;
}

$assocClass = $data[$name]->class;
$assocId = $data[$name]->identifier;
$isEagerLoad = ($assoc['fetch'] === ClassMetadata::FETCH_EAGER || ($assoc->isOneToOne() && ! $assoc->isOwningSide()));
$isEagerLoad = ($assoc->fetch === ClassMetadata::FETCH_EAGER || ($assoc->isOneToOne() && ! $assoc->isOwningSide()));

if (! $isEagerLoad) {
$data[$name] = $this->em->getReference($assocClass, $assocId);

continue;
}

$assocMetadata = $this->em->getClassMetadata($assoc['targetEntity']);
$assocMetadata = $this->em->getClassMetadata($assoc->targetEntity);
$assocKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocId);
$assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
$assocPersister = $this->uow->getEntityPersister($assoc->targetEntity);
$assocRegion = $assocPersister->getCacheRegion();
$assocEntry = $assocRegion->get($assocKey);

Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/Cache/DefaultQueryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public function put(QueryCacheKey $key, ResultSetMapping $rsm, mixed $result, ar
*/
private function storeAssociationCache(QueryCacheKey $key, AssociationMapping $assoc, mixed $assocValue): array|null
{
$assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
$assocPersister = $this->uow->getEntityPersister($assoc->targetEntity);
$assocMetadata = $assocPersister->getClassMetadata();
$assocRegion = $assocPersister->getCacheRegion();

Expand All @@ -321,7 +321,7 @@ private function storeAssociationCache(QueryCacheKey $key, AssociationMapping $a
return [
'targetEntity' => $assocMetadata->rootEntityName,
'identifier' => $assocIdentifier,
'type' => $assoc['type'],
'type' => $assoc->type(),
];
}

Expand All @@ -344,7 +344,7 @@ private function storeAssociationCache(QueryCacheKey $key, AssociationMapping $a

return [
'targetEntity' => $assocMetadata->rootEntityName,
'type' => $assoc['type'],
'type' => $assoc->type(),
'list' => $list,
];
}
Expand Down
517E
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public function __construct(
$this->metadataFactory = $em->getMetadataFactory();
$this->cacheLogger = $cacheConfig->getCacheLogger();
$this->hydrator = $cacheFactory->buildCollectionHydrator($em, $association);
$this->sourceEntity = $em->getClassMetadata($association['sourceEntity']);
$this->targetEntity = $em->getClassMetadata($association['targetEntity']);
$this->sourceEntity = $em->getClassMetadata($association->sourceEntity);
$this->targetEntity = $em->getClassMetadata($association->targetEntity);
}

public function getCacheRegion(): Region
Expand Down Expand Up @@ -135,7 +135,7 @@ public function containsKey(PersistentCollection $collection, mixed $key): bool
public function count(PersistentCollection $collection): int
{
$ownerId = $this->uow->getEntityIdentifier($collection->getOwner());
$key = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association['fieldName'], $ownerId);
$key = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association->fieldName, $ownerId);
$entry = $this->region->get($key);

if ($entry !== null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function afterTransactionRolledBack(): void
public function delete(PersistentCollection $collection): void
{
$ownerId = $this->uow->getEntityIdentifier($collection->getOwner());
$key = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association['fieldName'], $ownerId);
$key = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association->fieldName, $ownerId);

$this->persister->delete($collection);

Expand All @@ -53,7 +53,7 @@ public function update(PersistentCollection $collection): void
}

$ownerId = $this->uow->getEntityIdentifier($collection->getOwner());
$key = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association['fieldName'], $ownerId);
$key = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association->fieldName, $ownerId);

// Invalidate non initialized collections OR ordered collection
if ($isDirty && ! $isInitialized || $this->association->isOrdered()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function update(PersistentCollection $collection): void
if ($collection->isDirty() && $collection->getSnapshot()) {
throw CannotUpdateReadOnlyCollection::fromEntityAndField(
ClassUtils::getClass($collection->getOwner()),
$this->association['fieldName'],
$this->association->fieldName,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function afterTransactionRolledBack(): void
public function delete(PersistentCollection $collection): void
{
$ownerId = $this->uow->getEntityIdentifier($collection->getOwner());
$key = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association['fieldName'], $ownerId);
$key = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association->fieldName, $ownerId);
$lock = $this->region->lock($key);

$this->persister->delete($collection);
Expand All @@ -88,7 +88,7 @@ public function update(PersistentCollection $collection): void
$this->persister->update($collection);

$ownerId = $this->uow->getEntityIdentifier($collection->getOwner());
$key = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association['fieldName'], $ownerId);
$key = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association->fieldName, $ownerId);
$lock = $this->region->lock($key);

if ($lock === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ private function storeJoinedAssociations(object $entity): void

foreach ($this->class->associationMappings as $name => $assoc) {
if (
isset($assoc['cache']) &&
isset($assoc->cache) &&
($assoc->isToOne()) &&
($assoc['fetch'] === ClassMetadata::FETCH_EAGER || ! $assoc->isOwningSide())
($assoc->fetch === ClassMetadata::FETCH_EAGER || ! $assoc->isOwningSide())
) {
$associations[] = $name;
}
Expand All @@ -189,9 +189,9 @@ private function storeJoinedAssociations(object $entity): void
}

$assocId = $this->uow->getEntityIdentifier($assocEntity);
$assocMetadata = $this->metadataFactory->getMetadataFor($assoc['targetEntity']);
$assocMetadata = $this->metadataFactory->getMetadataFor($assoc->targetEntity);
$assocKey = new EntityCacheKey($assocMetadata->rootEntityName, $assocId);
$assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']);
$assocPersister = $this->uow->getEntityPersister($assoc->targetEntity);

$assocPersister->storeEntityCache($assocEntity, $assocKey);
}
Expand Down Expand Up @@ -544,9 +544,9 @@ public function refresh(array $id, object $entity, LockMode|int|null $lockMode =
/** @param array<string, mixed> $ownerId */
protected function buildCollectionCacheKey(AssociationMapping $association, array $ownerId): CollectionCacheKey
{
$metadata = $this->metadataFactory->getMetadataFor($association['sourceEntity']);
$metadata = $this->metadataFactory->getMetadataFor($association->sourceEntity);
assert($metadata instanceof ClassMetadata);

return new CollectionCacheKey($metadata->rootEntityName, $association['fieldName'], $ownerId);
return new CollectionCacheKey($metadata->rootEntityName, $association->fieldName, $ownerId);
}
}
8 changes: 4 additions & 4 deletions lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,15 +519,15 @@ protected function registerManaged(ClassMetadata $class, object $entity, array $
$id = [];

foreach ($class->identifier as $fieldName) {
$id[$fieldName] = isset($class->associationMappings[$fieldName])
? $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']]
$id[$fieldName] = isset($class->associationMappings[$fieldName]) && $class->associationMappings[$fieldName]->isToOneOwningSide()
? $data[$class->associationMappings[$fieldName]->joinColumns[0]['name']]
: $data[$fieldName];
}
} else {
$fieldName = $class->identifier[0];
$id = [
$fieldName => isset($class->associationMappings[$fieldName])
? $data[$class->associationMappings[$fieldName]['joinColumns'][0]['name']]
$fieldName => isset($class->associationMappings[$fieldName]) && $class->associationMappings[$fieldName]->isToOneOwningSide()
? $data[$class->associationMappings[$fieldName]->joinColumns[0]['name']]
: $data[$fieldName],
];
}
Expand Down
Loading
0