Skip to content

Commit

Permalink
some more fixes for mapping object vs arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher committed Feb 25, 2024
1 parent 8a8458e commit 84cb2f9
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions src/FieldDescription/FieldDescriptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\AssociationMapping;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\FieldMapping;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionFactoryInterface;
use Sonata\AdminBundle\FieldDescription\FieldDescriptionInterface;
use Symfony\Bridge\Doctrine\ManagerRegistry;
Expand All @@ -30,25 +31,15 @@ public function create(string $class, string $name, array $options = []): FieldD
{
[$metadata, $propertyName, $parentAssociationMappings] = $this->getParentMetadataForProperty($class, $name);

$associationMapping = $metadata->associationMappings[$propertyName] ?? [];

/**
* @psalm-suppress UndefinedClass
* @phpstan-ignore-next-line
*/
if ($associationMapping instanceof AssociationMapping) {
$associationMappingType = $associationMapping->type();
$associationMapping = (array) $associationMapping;
$associationMapping['type'] = $associationMappingType;
}

return new FieldDescription(
$name,
$options,
/** @psalm-suppress RedundantCastGivenDocblockType */
(array) ($metadata->fieldMappings[$propertyName] ?? []),
$associationMapping,
$parentAssociationMappings,
$this->mappingToArray($metadata->fieldMappings[$propertyName] ?? []),
$this->mappingToArray($metadata->associationMappings[$propertyName] ?? []),
array_map(
[$this, 'mappingToArray'],
$parentAssociationMappings,
),
$propertyName
);
}
Expand Down Expand Up @@ -109,4 +100,34 @@ private function getEntityManager(string $class): EntityManagerInterface

return $em;
}

/**
* @psalm-suppress UndefinedClass
* @phpstan-ignore-next-line
*/
private function mappingToArray(array|FieldMapping|AssociationMapping $mapping): array
{
if (\is_array($mapping)) {
return $mapping;
}

$arrayMapping = (array) $mapping;

/**
* @psalm-suppress UndefinedClass
* @phpstan-ignore-next-line
*/
if ($mapping instanceof AssociationMapping && !isset($arrayMapping['type'])) {
/* @phpstan-ignore-next-line */
$arrayMapping['type'] = $mapping->type();
}

if (isset($arrayMapping['joinColumns'])) {
foreach ($arrayMapping['joinColumns'] as $key => $joinColumn) {
$arrayMapping['joinColumns'][$key] = (array) $joinColumn;
}
}

return $arrayMapping;
}
}

0 comments on commit 84cb2f9

Please sign in to comment.