Sonataadminbundle: Validation error on creation of entity with a ID not auto generated cause system to think it's a update after

Created on 19 Apr 2016  路  5Comments  路  Source: sonata-project/SonataAdminBundle

| Question | Answer |
| --- | --- |
| Bundle version | dev-master 139d110 Symfony SonataAdminBundle |
| Symfony version | v2.8.3 The Symfony PHP framework |
| php version | 5.6.20 |

I have a entity that has a Id that is not auto generated.
When submitting a creation form if the validation failed I go back to the form input.
The only problem is that sonata think's I am editing the object since the id value is not null.
If resubmitting the form once the violation have been fix it will failed because sonata is not able to find the object.

Error message

unable to find the object with id : test2

Steps to reproduce

Create a entity that have a ID that is not auto generated (example a string).
Create the admin for it.
Filled the ID input.
Submit.
Make the validation failed for any reason.
Fix the violation on the next page.
Resubmit

Expected results

Should create the entity

Actual results

Entity is consider a existing instance so a update flow is choose instead of a creation flow.

The reason behind this is because the check done everywhere is admin.id(object) instead of being something like admin.isNew(object)

I think it's simple to explain but there is probably a lot of place to change this.

bug pending author stale

Most helpful comment

To be more accurate we should use

if ($entityManager->getUnitWork()->getEntityState($entity, UnitOfWork::STATE_NEW))

All 5 comments

I think it's simple to explain but there is probably a lot of place to change this.

Yeah, I once tried to use entities with application-generated identifiers and it failed somehow in almost every sonata bundle...

I think this would be something great for 3.0: replace all the

if ($entity->getId()) // and variations thereof

By

if ($entityManager->getUnitOfWork()->contains($entity))

To be more accurate we should use

if ($entityManager->getUnitWork()->getEntityState($entity, UnitOfWork::STATE_NEW))

There are several possible model managers so I guess it would be even more abstract

Yes, it should belong to ModelManagerInterface and leave the implementation to the various persistence bundles.

Refs:

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

Was this page helpful?
0 / 5 - 0 ratings