jagilpe / encryption-bundle

A Symfony Bundle to encrypt sensitive data before persisting it
MIT License
3 stars 1 forks source link

Problème form type entity query_builder #1

Open kshataer opened 6 years ago

kshataer commented 6 years ago

Hello,

I got un error fatal when i try to crete a EntityType Field in a form like

->add(
'MyEntity',
EntityType::class,
array( 'class' => MyEntity::class,
'query_builder' => function (MyEntityRepository $er) { return $er->createQueryBuilder('a') ->orderBy('a.id', 'ASC')
})

Type error: Argument 1 passed to AppBundle\Form\MyFormType::AppBundle\Form{closure}() must be an instance of AppBundle\Repository\MyEntityRepository, instance of Jagilpe\EncryptionBundle\Doctrine\ORM\EncryptionEntityRepositoryDecorator given

Do you have a solution for this issues? Please.

jagilpe commented 6 years ago

The way the encryption and decryption of the entities in the bundle work is through the use of a decorator for the object repository, that intercepts each query and then decrypts or encrypts the data depending if we want to read or save the data. That limits the way the repositories can be used to access the data. The decorator class (Jagilpe\EncryptionBundle\Doctrine\ORM\ EncryptionEntityRepositoryDecorator) implements only the Doctrine\Common\Persistence\ObjectRepository and the Doctrine\Common\Collections\Selectable interfaces. If you put ObjectRepository or Selectable instead of MyEntityRepository in your closure, you should not get the error, but then you won't be able to use the createQueryBuilder method.

To support filtering and sorting your queries you have to use the filtering API of the Collections (http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#filtering-collections). In other case you will get the wrong results because the filters and the sorting will be applied before the data is decrypted.

In your case I think that the following should work:

use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Selectable;
//...
->add(
'MyEntity',
EntityType::class,
array( 'class' => MyEntity::class,
'query_builder' => function (Selectable $er) {
$criteria = Criteria::create();
$criteria->orderBy(array("id" => Criteria::ASC));
return $er->matching($criteria);
})

$er will still be your entity repository, but in this case you will use it through the Selectable interface

kshataer commented 6 years ago

Thank you jagilpe, il will test your soluction, after all thank for your bundle, il help a lot 👍