pomm-project / ModelManager

Model Manager for the Pomm database framework.
MIT License
66 stars 27 forks source link

Get model class from entity #59

Open SebLours opened 8 years ago

SebLours commented 8 years ago

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

chanmix51 commented 8 years ago

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?

SebLours commented 8 years ago

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.

chanmix51 commented 8 years ago

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

SebLours commented 8 years ago

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)

chanmix51 commented 8 years ago

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:

SebLours commented 8 years ago

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 ?