DoctrineEncryptBundle icon indicating copy to clipboard operation
DoctrineEncryptBundle copied to clipboard

@UniqueEntity

Open etienne-cartong opened this issue 4 years ago • 1 comments

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 ?

etienne-cartong avatar Dec 16 '21 14:12 etienne-cartong

@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.

cs-akash-jarad avatar Dec 19 '21 18:12 cs-akash-jarad