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

@UniqueEntity #58

Open
etienne-cartong opened this issue Dec 16, 2021 · 1 comment
Open

@UniqueEntity #58

etienne-cartong opened this issue Dec 16, 2021 · 1 comment

Comments

@etienne-cartong
Copy link

etienne-cartong commented Dec 16, 2021

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants