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

Fix field name generation and accept string identifier for filter entity #21

Merged
merged 1 commit into from
Jul 31, 2024
Merged
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
9 changes: 6 additions & 3 deletions Event/Subscriber/DoctrineORMSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Spiriit\Bundle\FormFilterBundle\Event\Subscriber;

use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
Expand Down Expand Up @@ -86,7 +87,7 @@ public function filterEntity(GetFilterConditionEvent $event)

if ($dqlFrom = $event->getQueryBuilder()->getDQLPart('from')) {
$rootPart = reset($dqlFrom);
$fieldName = ltrim($event->getField(), $rootPart->getAlias() . '.');
$fieldName = \str_replace(\sprintf('%s.', $rootPart->getAlias()), null, $event->getField());
Copy link
Contributor

Choose a reason for hiding this comment

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

str_replace works better than ltrim in some case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using ltrim method is not correct here. ltrim replace all listing characters and not word.

Example, 2 entities :

  1. Document with alias document
  2. Category with alias category
dump($event->getField()); // document.category
dump($rootPart->getAlias() . '.'); // document.

// Before (wrong)
dump(ltrim($event->getField(), $rootPart->getAlias() . '.')); // ategory

// After (good)
dump(\str_replace(\sprintf('%s.', $rootPart->getAlias()), null, $event->getField())); // category

$metadata = $queryBuilder->getEntityManager()->getClassMetadata($rootPart->getFrom());

if (isset($metadata->associationMappings[$fieldName]) && (!$metadata->associationMappings[$fieldName]['isOwningSide'] || $metadata->associationMappings[$fieldName]['type'] === ClassMetadataInfo::MANY_TO_MANY)) {
Expand All @@ -108,13 +109,15 @@ public function filterEntity(GetFilterConditionEvent $event)
if (count($ids) > 0) {
$event->setCondition(
$expr->in($filterField, ':' . $paramName),
[$paramName => [$ids, Connection::PARAM_INT_ARRAY]]
[$paramName => [$ids, \is_int($ids[0]) ? ArrayParameterType::INTEGER : ArrayParameterType::STRING]]
);
}
} else {
$id = $this->getEntityIdentifier($values['value'], $queryBuilder->getEntityManager());

$event->setCondition(
$expr->eq($filterField, ':' . $paramName),
[$paramName => [$this->getEntityIdentifier($values['value'], $queryBuilder->getEntityManager()), Types::INTEGER]]
[$paramName => [$id, \is_int($id) ? Types::INTEGER : Types::STRING]]
);
}
}
Expand Down
Loading