I don't know if this is correct, but I implemented this recently by creating a custom mapping type according to the Doctrine docs. Something like the following:
class EncryptedStringType extends TextType { const MYTYPE = 'encryptedstring'; // modify to match your type name public function convertToPHPValue($value, AbstractPlatform $platform) { return base64_decode($value); } public function convertToDatabaseValue($value, AbstractPlatform $platform) { return base64_encode($value); } public function getName() { return self::MYTYPE; } }
I registered this type in the package class:
class MyOwnBundle extends Bundle { public function boot() { $em = $this->container->get("doctrine.orm.entity_manager"); try { Type::addType("encryptedstring", "My\OwnBundle\Type\EncryptedStringType"); $em-> getConnection()-> getDatabasePlatform()-> registerDoctrineTypeMapping("encryptedstring", "encryptedstring"); } catch (\Doctrine\DBAL\DBALException $e) {
and then I was able to refer to it when creating my objects, for example:
protected $name;
It was a quick implementation, so I would be interested to know the correct way to do it. I also assume that your encryption service is something accessible from the container; I don't know how much it is possible / possible to pass services to user types this way: ...) -
source share