ModelManager icon indicating copy to clipboard operation
ModelManager copied to clipboard

Get model class from entity

Open SebLours opened this issue 9 years ago • 6 comments

I would like to get the model class from a flexible entity instance ... Is there a simple way to do this ?

I need this for my custom UniqueFlexibleEntityValidator which validate that a database record doesn't exists for a field set (like a slug). It is what the DoctrrineUniqueEntityValidator does (@see https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php). I need to access to the model class from the flexible entity instance in the validation process (to test uniqueness).

I can add a custom constant (MODEL_CLASS) in my flexible entity class but it is not optimized ...

Thanks

SebLours avatar Jan 29 '16 16:01 SebLours

Entity instances are database agnostic. With Pomm, it is possible to have multiple Model class to hydrate the same entity class (useful to manage users, operators, administrators by example).

This said, you are pointing at a problem in the actual model manager: there is no way to know at least what model class has created an entity (if that happened).

Implementing a signature in the entities would make your Validator to know which model class to use to call the built in existWhere method.

Would that be a solution to your problem?

chanmix51 avatar Jan 30 '16 13:01 chanmix51

I have solved my problem with a custom ModelClassResolver which resolve the model class from an entity class. I use it in my validator and i get the model with the session, all works fine.

I already think that it will be great if the model manager could provide the model class from an entity.

SebLours avatar Jan 31 '16 10:01 SebLours

Is it possible to have a look at the ModelClassResolver piece of code ? (in a gist or so)

chanmix51 avatar Feb 01 '16 09:02 chanmix51

It's a very simple service ... https://gist.github.com/SebLours/3375af85dcafe0d6add4

In my model classes i have set the entity class as a constant. Every model class must be passed as arguments to this service ... it's not very convenient but that's solved my problem ... :o)

SebLours avatar Feb 01 '16 18:02 SebLours

I think I will go for the signature idea. All model class sign the entity they create or fetch. It might be interesting to do that in the identity mapper to prevent it to return the same entity when two different model classes (with different projection) do query the same entities.

Thank you very much for your report :+1:

chanmix51 avatar Feb 02 '16 09:02 chanmix51

Your idea is to add a createdBy method in the FlexibleEntity class ?

The model can call it on entity creation, but the HydrationPlan create entity itself without calling the Model.

Must we inject the Model in the HydrationPlan ?

SebLours avatar Feb 25 '16 11:02 SebLours