Skip to content

Commit

Permalink
bug #5458 Fix: Search (silasjoisten)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 4.x branch.

Discussion
----------

Fix: Search

closes #5456
closes #5452

In following scenario i could reproduce the issue:

// src/Entity/User.php
```php
<?php

declare(strict_types=1);

namespace App\Entity;

use App\Domain\Identifier\UserId;
use App\Repository\UserRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Table;

#[Entity(repositoryClass: UserRepository::class)]
#[Table(name: '`users`')]

class User
{
    #[Id]
    #[Column(type: 'user_id', unique: true)]
    private UserId $id;

    #[Column(type: Types::STRING)]
    private string $name;

    #[Column(type: Types::STRING, unique: true)]
    private string $email;

    /**
     * `@var` string[]
     */
    #[Column(type: Types::JSON)]
    private array $roles = [];

// etc...
}
```

//src/Controller/Admin/UserCrudController.php
```php
<?php

declare(strict_types=1);

namespace App\Controller\Admin;

use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;

final class UserCrudController extends AbstractCrudController
{
    public function configureCrud(Crud $crud): Crud
    {
        return $crud
            ->setDefaultSort([
                'id' => 'DESC',
            ])
            ->setEntityLabelInSingular('User')
            ->setEntityLabelInPlural('Users')
            ->setSearchFields([
                'id',
                'email',
                'name',
                'roles',
            ])
            ->setPaginatorPageSize(100)
            ->overrideTemplates([
                'crud/edit' => 'admin/pages/user/edit.html.twig',
            ]);
    }

//etc ...
}
```
If i remove `roles` (type `json`) from the `setSearchFields()` array it works without any exception. With this Json type i get an exception, which is described in the issues mentioned before.

Here is the fix that JSON is getting also checked.
The funny part: Even though with or without this fix i am not able to search any `json` property.

This fixes the Exception but you are still not able to search through `json`.

Commits
-------

00fbcec Fix: Search
  • Loading branch information
javiereguiluz committed Oct 31, 2022
2 parents f15adc8 + 00fbcec commit 2bb044c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Orm/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,24 @@ private function addSearchClause(QueryBuilder $queryBuilder, SearchDto $searchDt
$isTextProperty = \in_array($propertyDataType, ['string', 'text', 'citext', 'array', 'simple_array']);
$isGuidProperty = \in_array($propertyDataType, ['guid', 'uuid']);
$isUlidProperty = 'ulid' === $propertyDataType;
$isJsonProperty = 'json' === $propertyDataType;

if (!$isBoolean &&
!$isSmallIntegerProperty &&
!$isIntegerProperty &&
!$isNumericProperty &&
!$isTextProperty &&
!$isGuidProperty &&
!$isUlidProperty
!$isUlidProperty &&
!$isJsonProperty
) {
$idClassName = (new \ReflectionProperty($entityDto->getFqcn(), $propertyName))->getType()->getName();
$idClassType = (new \ReflectionProperty($entityDto->getFqcn(), $propertyName))->getType();

$isUlidProperty = (new \ReflectionClass($idClassName))->isSubclassOf(Ulid::class);
$isGuidProperty = (new \ReflectionClass($idClassName))->isSubclassOf(Uuid::class);
if (null !== $idClassType) {
$idClassName = $idClassType->getName();
$isUlidProperty = (new \ReflectionClass($idClassName))->isSubclassOf(Ulid::class);
$isGuidProperty = (new \ReflectionClass($idClassName))->isSubclassOf(Uuid::class);
}
}

// this complex condition is needed to avoid issues on PostgreSQL databases
Expand Down

0 comments on commit 2bb044c

Please sign in to comment.