-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Issue with validator service on unique entities in collections #17884
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
Comments
You could put a constraint on your collection which specifies that the titles have to be unique. This is not a bug in the unique entity constraint afaik as it only checks the database. |
@iltar The constraint is on the collection. However as the data is duplicated. I imagine that the database is only checked a single time for performance reasons so on the second insert the database will fail. |
The constraint doesn't know that there is another object containing the same data somewhere in your object graph. And as all validation happens before any of your data is written to the database it cannot catch this kind of error, but you have to implement that yourself as a separate constraint on the collection. |
@xabbuh should this not be implemented into the unique constraint to check data to be persisted aswel. To me it wouldn't seem to make sense that the unique constraint would fail to check all data with itself and the database before being persisted as there would be nothing benificial that could be gained by not adding the check to the constraint. Would it be feasible to create a PR on the unique constraint to add this feature in? Or would this not be generic enough to resolve any specific problem? |
@liamsorsby I don't see how you would implement it in the unique constraint. But if you have an idea, feel free to submit a pull request. |
@xabbuh I may be wrong but isn't the unique constraint to be checked against the data in the form of an array. If so then couldn't I just do an array search on that data? To be honest I've not actually had a look at the internals of how the unique constraint works to be honest. But I will certainly give it a go. |
@liamsorsby The unique entity constraint checks that there is no existing entity in your database that already contains the same value(s) for a particular property (or a set of properties). |
What you probably want is something like this: class FooData
{
/**
* @Assert\Unique(fields={"username"})
* @var MyUser[]
*/
private $myUsers = [];
} As opposing to the unique entity constraint -which checks just that one field in the database- you want a constraint that checks all the fields in all items of the collection for that constraint. |
@iltar would this be instead of the unique entity check Or alongside the unique entity check? Is the unique entity check designed so it doesn't check for unique data in the array collection before persisting? The issue I'm having is a collection with duplicate data being persisted and failing due to duplicates in the data that hasn't been persisted yet. |
@liamsorsby in theory you can make the unique constraint do this (but requires a PR) |
my solution for checking same item in collection: CollectionSameItem.php
CollectionSameItemValidator.php
Example of usage: example1:
message -> User "Firstname Secondname" already exist in project example2:
message -> User "Firstname" already exist in project example3:
message -> This user already exist this project |
Looking for something like this too. |
Duplicity of #9888. Linked issue contains proposal for Unique constraint. |
Closing as a duplicate then. |
We seem to be having an issue with the validator service when using collections and the validator to check for unique entities.
The validator will correctly find any issues before persisting the entity to the doctrine manager.
The validator seems to check the data in the current database but doesn't check the data that is going to be saved.
For instance if title is a unique entity and in my collection I am adding two new collections to the database both with the title abc it will not throw an error with the validator but will try to add them both causing an internal server error.
We are adding clientside checks but wasn't sure if this was a bug in the validator service.
The text was updated successfully, but these errors were encountered: