I am working onbuilding an application that is becoming some kind of social media platform. I need to work a lot with the entities ID's in the HTML for the purpose of AJAX calls. However, for security reasons I don't want the original ID's exposed.
Instead of needing to encryptencrypting an ID every time it is displayed, I figured it would be better to encrypt the ID of every entity once, when they are loaded. I tried to encrypt the ID's$id field of entities on postLoad and decrypt them again at preUpdate. That works fine, until you need to work with relations come around. Doctrine simply finds no relations because the ID of the current entity is ecrypted (duh).
I decided it would be better to not touch the ID$id field of the entities and create an additional field. The field will only be used when the entity is loaded and will not persistbe persisted. Instead of adding the field and get/set methods to each entity, I decided to create an interface and abstract class. The interface is to use for checking the object later on. The abstract class is to implement the methods.
This is the interface defining the methods that are needed. I also defined getId() here. This is so I can be sure that getId() is available by checking the objects against this interface.
The Doctrine listener class encrypts the ID of the entity and sets the field when the entity is loaded. Read more about Doctrine event listeners. I use NzoUrlEncryptor because I also use it for routing purposes. It could however be any encryption tool.