StrangeElement
StrangeElement

Reputation: 2302

How to add user-defined methods to Doctrine entities

What is the official pattern to add user-defined methods to Doctrine ORM entities? Basically to add object-specific methods, like settings defaults, doing private object processing.

I tried extending my entities but then whenever I interact with Doctrine, I would get the Entity object, not the child object that extends it. There are superclass and single-class and other patterns that try to solve other problems, not merely adding methods to a persistent object.

I don't want an entity extending another entity. I just want to be able to add methods to the persistent entity object.

Ideally I would have A extends A_Entity, with A_Entity being the auto-generated entity from XML mappings, and A being the object I use in the application. But this doesn't even seem possible in Doctrine.

Upvotes: 1

Views: 528

Answers (1)

Sergei Kutanov
Sergei Kutanov

Reputation: 825

I think you can just add methods within entity class itself. If you make those methods public you can even use them in twig template directly. If you regenerate getters and setters afterwards with console command (generate:doctrine:entities) your custom methods won't be overwritten.

Upvotes: 1

Related Questions