Configurable dynamic Doctrine database entities in Symfony 2 Configurable dynamic Doctrine database entities in Symfony 2 symfony symfony

Configurable dynamic Doctrine database entities in Symfony 2


Is it possible to have fully configurable dynamic Doctrine entities?

Doctrine generates proxy classes for you entities. That means that doctrine generates PHP code with class, which extends your Entity class and overrides the methods - puts some custom logic and then calls the parent method.

So, I think that the only way to make this really happen is to generate the PHP code for entities in your code. That is, every time entity is created in your website, you should generate PHP file with that entity, then run migrations.

Am I on the right track with my approach? If not, could you suggest an alternative?

I don't think that you should use Doctrine ORM at all in this case, at least in the way you're trying to do that.

Generally, ORM is used for easier/more manageable programming. That is, you can set relations, use lazy-loading, unit of work (change entity properties and then just flush) etc. If your entities are generated dynamically, what features will you use at all? Developer will not write code for these entities, because, as you've said, there is no way to know what fields it will have.

You haven't provided concrete use-case - why do you want to do that in the first place. But I imagine that it could be really done in some easier way.

If users can store any structure at all, should you use MySQL at all? ElasticSearch or similar solutions could be really much better in such cases.

How could I have truly dynamic class properties? Or should I be generating new Doctrine entity PHP classes whenever the users change the entity configuration?

As I've mentioned - yes. Unless you would want to override or replace some of Doctrine code, but I imagine it could be lots of it (proxy classes etc.)