There’s isDefault
property in an entity. If it’s set to true, the isDefault
property of all other entities must be set to false
.
Is there a clean solution without the QueryBuilder or plain SQL? And if so, what is the solution.
TLDR: How can I update one ore more values of any entity whose ID IS NOT a specific one?
Update
I have a table of entities. Only one entity can have a (bool) isDefault
status. If I create a new entity, which can be set to isDefault = true
all other entities must be set to isDefault = false
.
2
Answers
The solution is not to update all other entities whose state can already be
isDefault = false
but to update only those which have the stateisDefault = true
.It would be possible to use
findOneBy()
but this is the more safe approach to prevent multiple entities with the sameisDefault = true
status.You can mark the relation to cascade-all and then use setters with persist to persist updates.
Read Doctrine: Transitive persistence / Cascade Operation for more info.