I'm using Symfony 2 with Doctrine 2.
I need to encrypt a field in my entity using an encryption service, and I'm wondering where should I put this logic.
I'm using a Controller > Service > Repository architecture.
I was wondering if a listener would be a good idea, my main concern is, if my entity is stored encrypted, if I decrypt it on the fly its state it's gonna be changed and I'm not sure it's a good idea.
How would you implement this?
To expand on richsage and targnation's great answers, one way to inject a dependency (e.g., cypto service) into a custom Doctrine mapping type, could be to use a static property and setter:
Configuration would look like this:
I don't know if it's the right way at all, but I implemented this recently by creating a custom mapping type, as per the Doctrine docs. Something like the following:
I registered this type in my bundle class:
and then I was able to reference it when creating my entities, eg:
This was a quick implementation, so I'd be interested to know the correct way of doing it. I presume also that your encryption service is something available from the container; I don't know how feasible/possible it would be to pass services into custom types this way either... :-)
richsage's answer was pretty good, except I wouldn't register the custom type in the bundle class file. It's recommended that you use the config.yml like so:
Then just make sure in your EncryptedStringType class you specify the getName function to return encrypted_string.
Now in your model definition (or annotation) you can use the encrypted_string type.