Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/Field/Configurator/CommonPreConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldConfiguratorInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use EasyCorp\Bundle\EasyAdminBundle\Factory\EntityFactory;
use EasyCorp\Bundle\EasyAdminBundle\Field\AvatarField;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
Expand All @@ -21,10 +22,12 @@
final class CommonPreConfigurator implements FieldConfiguratorInterface
{
private PropertyAccessorInterface $propertyAccessor;
private EntityFactory $entityFactory;

public function __construct(PropertyAccessorInterface $propertyAccessor)
public function __construct(PropertyAccessorInterface $propertyAccessor, EntityFactory $entityFactory)
{
$this->propertyAccessor = $propertyAccessor;
$this->entityFactory = $entityFactory;
}

public function supports(FieldDto $field, EntityDto $entityDto): bool
Expand Down Expand Up @@ -153,6 +156,26 @@ private function buildSortableOption(FieldDto $field, EntityDto $entityDto): boo
return $isSortable;
}

// if it's an association, check recursively if the nested property exists
if ($entityDto->isAssociation($field->getProperty())) {
$associatedProperties = explode('.', $field->getProperty());
for ($i = 0; $i < \count($associatedProperties); $i++) {
$property = $associatedProperties[$i];
if (!$entityDto->hasProperty($property)) {
return false;
}

// try to get the entity associated to the property, except for the last property
if (isset($associatedProperties[$i + 1])) {
$propertyMetadata = $entityDto->getPropertyMetadata($property);
$targetEntity = $propertyMetadata->get('targetEntity');
$entityDto = $this->entityFactory->create($targetEntity);
}
}

return true;
}
Comment on lines +160 to +177
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the logic to handle associations is re implemented in many places and we should find a way to implement it in a reusable way across searching, filtering and ordering.

Another implementation supporting embedded is here:

private function getSearchablePropertiesConfig(QueryBuilder $queryBuilder, SearchDto $searchDto, EntityDto $entityDto): array


return $entityDto->hasProperty($field->getProperty());
}

Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@

->set(CommonPreConfigurator::class)
->arg(0, new Reference('property_accessor'))
->arg(1, service(EntityFactory::class))
->tag(EasyAdminExtension::TAG_FIELD_CONFIGURATOR, ['priority' => 9999])

->set(CountryConfigurator::class)
Expand Down