Skip to content
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

Relation filter #6664

Open
maxkain opened this issue Dec 22, 2024 · 1 comment
Open

Relation filter #6664

maxkain opened this issue Dec 22, 2024 · 1 comment

Comments

@maxkain
Copy link

maxkain commented Dec 22, 2024

I have done filter with relations. Maybe it will be nice to add something like this.
Ho to use, for example, for article entity with comments:

    public function configureFilters(Filters $filters): Filters
    {
        $commentCreatedAtFilter = DateTimeFilter::new('createdAt');

        return $filters
            ->add(RelationFilter::new('comments', $commentCreatedAtFilter, 'Comment created at'));
    }

Implementation:

namespace App\Utils\EasyAdmin\Filter;

use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Filter\FilterInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterDataDto;
use EasyCorp\Bundle\EasyAdminBundle\Filter\FilterTrait;

final class RelationFilter implements FilterInterface
{
    use FilterTrait;

    private FilterInterface $wrappedFilter;

    public function setWrappedFilter(FilterInterface $wrappedFilter): static
    {
        $this->wrappedFilter = $wrappedFilter;
        return $this;
    }

    public static function new(string $propertyName, FilterInterface $wrappedFilter, $label = null): self
    {
        $wrapped = $wrappedFilter->getAsDto();
        $wrapped->setFormTypeOption('mapped', false);

        return (new self())
            ->setWrappedFilter($wrappedFilter)
            ->setFormType($wrapped->getFormType())
            ->setFormTypeOptions($wrapped->getFormTypeOptions())
            ->setFilterFqcn(__CLASS__)
            ->setProperty($propertyName)
            ->setLabel($label)
        ;
    }

    public function apply(QueryBuilder $queryBuilder, FilterDataDto $filterDataDto, ?FieldDto $fieldDto, EntityDto $entityDto): void
    {
        $alias = $filterDataDto->getEntityAlias();
        $property = $filterDataDto->getProperty();
        $comparison = $filterDataDto->getComparison();
        $value = $filterDataDto->getValue();
        $value2 = $filterDataDto->getValue2();
        $wrappedFilter = $this->wrappedFilter;
        $relationAlias = $property;

        $this->addJoinIfNotExists($queryBuilder, $alias, $property, $relationAlias);

        $formData = ['comparison' => $comparison, 'value' => $value, 'value2' => $value2];
        $wrappedFilterDataDto = FilterDataDto::new(1, $wrappedFilter->getAsDto(), $relationAlias, $formData);
        $wrappedFilter->apply($queryBuilder, $wrappedFilterDataDto, $fieldDto, $entityDto);
    }

    private function addJoinIfNotExists(QueryBuilder $queryBuilder, string $alias, string $property, string $relationAlias): void
    {
        if (!$this->hasJoin($queryBuilder, $relationAlias)) {
            $queryBuilder->innerJoin(sprintf('%s.%s', $alias, $property), $relationAlias);
        }
    }

    private function hasJoin(QueryBuilder $queryBuilder, string $relationAlias): bool
    {
        $joins = $queryBuilder->getDQLPart('join')['entity'] ?? [];
        $hasJoin = false;
        foreach ($joins as $join) {
            if ($join instanceof Join) {
                if ($join->getAlias() == $relationAlias) {
                    $hasJoin = true;
                    break;
                }
            }
        }

        return $hasJoin;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@maxkain and others