Unique validator fails on 2nd submission #6711

Open
opened 2026-01-22 15:37:23 +01:00 by admin · 0 comments
Owner

Originally created by @herbyxxx on GitHub (May 10, 2021).

I have this issue in my Symfony project, however the problem seems to be related to doctrine (already created issue in symfony github for reference).

Description
I am using the Symfony UniqueEntity Validator which uses the doctrine.orm.validator.unique service with the findBy method.

I have one Entity called ArticleTranslation which has two fields called article (ManyToOne to Entity Article) and locale (language for the translation e.g. 'en' or 'de'). Those fields are unique together (only one translation per language) using @UniqueEntity(fields={"content", "locale"}). My ArticleTranslations are persisted by using cascade={"persist"} on the Article class (my article form has 1 default translation which needs to be added - so there is atleast 1 translation available).

So what I did notice is, that on dev mode everything works like a charm. No issues at all. But whenever I switch to prod mode the first form submission is ok. However on the 2nd submission the @UniqueEntity(fields={"content", "locale"}) fails. When you clear the cache after that php bin/console cache:clear it suddenly works for one time. The 2nd one fails again.

I did debug the UniqueEntityValidator and did indeed notice some strange behavior. The data itself is always the same:

  • One ArticleTranslation class with id = NULL (because it is a new object), locale = 'de' and the Article from the relation
  • The Article itself also has id = NULL (because it is also new, they are both created at the same time)

However the findBy method (used by the validator) seems to be different at the 2nd submission. I logged my SQL queries and did notice the following:

//First submission = OK
SELECT t0.id AS id_1, t0.title AS title_2, t0.body AS body_3, t0.locale AS locale_4, t0.created_at AS created_at_5, t0.updated_at AS updated_at_6, t0.article_id AS article_id_7, t0.created_by_id AS created_by_id_8, t0.updated_by_id AS updated_by_id_9 FROM article_translation t0 WHERE t0.article_id = NULL AND t0.locale = 'de'

Here we notice that the WHERE is correct WHERE t0.article_id = NULL AND t0.locale = 'de' (no result found => Unique passes)
This changes on the second submission though.

//Second submission = FAIL
SELECT t0.id AS id_1, t0.title AS title_2, t0.body AS body_3, t0.locale AS locale_4, t0.created_at AS created_at_5, t0.updated_at AS updated_at_6, t0.article_id AS article_id_7, t0.created_by_id AS created_by_id_8, t0.updated_by_id AS updated_by_id_9 FROM article_translation t0 WHERE t0.article_id = 1 AND t0.locale = 'de'

with the WHERE beeing suddenly WHERE t0.article_id = 1 AND t0.locale = 'de' (even though the Article ID is still NULL in the data).

Again, after clearing the cache it works once. I really have no idea whats causing that and have a hard time to find the issue. Also as mentioned before in dev mode everything works correct (so maybe it is a cache related?)

How to reproduce
Reproduce example: https://github.com/herbyxxx/issue_40832


Is there any way to fix this in doctrine? Thanks

Originally created by @herbyxxx on GitHub (May 10, 2021). I have this issue in my Symfony project, however the problem seems to be related to doctrine ([already created issue in symfony github for reference](https://github.com/symfony/symfony/issues/40832)). **Description** I am using the Symfony *UniqueEntity* Validator which uses the `doctrine.orm.validator.unique` service with the `findBy` method. I have one Entity called `ArticleTranslation` which has two fields called `article` (ManyToOne to Entity Article) and `locale` (language for the translation e.g. 'en' or 'de'). Those fields are unique together (only one translation per language) using `@UniqueEntity(fields={"content", "locale"})`. My `ArticleTranslations` are persisted by using `cascade={"persist"}` on the `Article` class (my article form has 1 default translation which needs to be added - so there is atleast 1 translation available). So what I did notice is, that on `dev` mode everything works like a charm. No issues at all. But whenever I switch to `prod` mode the first form submission is ok. However on the 2nd submission the `@UniqueEntity(fields={"content", "locale"})` fails. When you clear the cache after that `php bin/console cache:clear` it suddenly works for **one time**. The 2nd one fails again. I did debug the `UniqueEntityValidator` and did indeed notice some strange behavior. The data itself is always the same: - One ArticleTranslation class with id = NULL (because it is a new object), locale = 'de' and the Article from the relation - The Article itself also has id = NULL (because it is also new, they are both created at the same time) However the `findBy` method (used by the validator) seems to be different at the 2nd submission. I logged my SQL queries and did notice the following: ``` //First submission = OK SELECT t0.id AS id_1, t0.title AS title_2, t0.body AS body_3, t0.locale AS locale_4, t0.created_at AS created_at_5, t0.updated_at AS updated_at_6, t0.article_id AS article_id_7, t0.created_by_id AS created_by_id_8, t0.updated_by_id AS updated_by_id_9 FROM article_translation t0 WHERE t0.article_id = NULL AND t0.locale = 'de' ``` Here we notice that the WHERE is correct `WHERE t0.article_id = NULL AND t0.locale = 'de'` (no result found => Unique passes) This changes on the second submission though. ``` //Second submission = FAIL SELECT t0.id AS id_1, t0.title AS title_2, t0.body AS body_3, t0.locale AS locale_4, t0.created_at AS created_at_5, t0.updated_at AS updated_at_6, t0.article_id AS article_id_7, t0.created_by_id AS created_by_id_8, t0.updated_by_id AS updated_by_id_9 FROM article_translation t0 WHERE t0.article_id = 1 AND t0.locale = 'de' ``` with the WHERE beeing suddenly `WHERE t0.article_id = 1 AND t0.locale = 'de'` (even though the Article ID is still NULL in the data). Again, after clearing the cache it works once. I really have no idea whats causing that and have a hard time to find the issue. Also as mentioned before in `dev` mode everything works correct (so maybe it is a cache related?) **How to reproduce** Reproduce example: https://github.com/herbyxxx/issue_40832 ___ Is there any way to fix this in doctrine? Thanks
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6711