Skip to content
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

Get model class from entity #59

Open
SebLours opened this issue Jan 29, 2016 · 6 comments
Open

Get model class from entity #59

SebLours opened this issue Jan 29, 2016 · 6 comments

Comments

@SebLours
Copy link

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 SebLours changed the title Get model class from entiot Get model class from entity Jan 29, 2016
@chanmix51
Copy link
Member

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
Copy link
Author

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
Copy link
Member

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

@SebLours
Copy link
Author

SebLours commented Feb 1, 2016

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
Copy link
Member

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 👍

@chanmix51 chanmix51 added this to the 2.1 milestone Feb 2, 2016
@SebLours
Copy link
Author

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants