We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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; } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
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:
Implementation:
The text was updated successfully, but these errors were encountered: