absolute-quantum / DoctrineEncryptBundle

:key: Encrypted symfony entities by verified and standardized libraries
https://packagist.org/packages/michaeldegroot/doctrine-encrypt-bundle
MIT License
142 stars 74 forks source link

@UniqueEntity #58

Open etienne-cartong opened 2 years ago

etienne-cartong commented 2 years ago

It might not been an issue, but it's something I'm struggling with.

I'm working a project based on Symfony 4. I'm trying to make the @UniqueEntity works on an @Encrypted field, and I can't figure out how.

Without the @Encrypted annotation, the @UniqueEntity annotation prevents the duplication With the @Encrypted annotation, the @UniqueEntity annotation allows the duplication I feel like I'm missing something obvious here :)

/**
 * @ORM\Entity(repositoryClass="App\Repository\DemoRepository")
 * @ORM\HasLifecycleCallbacks()
 * @UniqueEntity(
 *      fields={"example"},
 *      ignoreNull=true,
 * )
 *
 */
class Demo implements LoggableEntityInterface
{
    /**
     * @ORM\Column(type="text", nullable=true)
     * @Encrypted
     */
    private $example;

Any idea ?

Maybe I'm not doing this right. But I want to encrypt stuff in my db and still avoid duplication. How else could I tackle this ?

cs-akash-jarad commented 2 years ago

@etienne-cartong you need to implement event listeners on the Demo entity (https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#implementing-event-listeners). as when you encrypt the data and when it reach DB for persisting, it's already encrypted and every time you encrypt the data, it returns a different string every time. so at the DB level, the Unique constrain is honored. so you have to check yourself in code instead of relying on the UniqueEntity annotation.