Hello everyone,

I have a problem I don't succeed to resolve.
I have a *Crypter *class meant to *encrypt/decrypt *data : it has a 
*encode($value)* method, a *decode($value)* method, and a $*key *property 
required in the constructor. This key is stored in my *parameters.yml *and 
depends on the project.

As I want my data to be encrypted when my entity is persisted, and 
decrypted when loaded, I have to define a custom Doctrine type. 

So I wrote a *DoctrineCrypter *class that extends *Doctrine\DBAL\Types\Type*
. 
It has a getter / setter for accessing the *Crypter *class, and 
*convertToPhpValue($value)* simply leads to 
*$this->getCrypter()->decode($value)*, while 
*convertToDatabaseValue($value)* is a shortcut to 
*$this->getCrypter()->encode($value)*.

All right. Now, how to inject the *Crypter *class into the *DoctrineCrypter 
*type, since the constructor of the parent class *Doctrine\DBAL\Types\Type *has 
been declared *final *and *private *by Doctrine ?

Well, what I have to do is to register the type first via *Type::addType()*, 
then access it via* Type::getType()* for calling *setCrypter()* with the 
configured *Crypter *class. And afterwards, register the type mapping to 
Doctrine :

if (!Type::hasType('sbs_crypter'))
    Type::addType('sbs_crypter', 
'DoctrineCrypter');Type::getType('sbs_crypter')->setCrypter($container->get('crypter_object'));

$connection     =   $this->container->get('doctrine.dbal.default_connection');
$connection->getDatabasePlatform()->registerDoctrineTypeMapping('sbs_crypter', 
'sbs_crypter');


The problem is : *where do I put this code* ? If I hook this to 
*kernel.request* with an event listener, it works pretty well. But in HTTP 
only. When I use the console (doctrine:schema:update for instance), I get 
the following exception :

[Doctrine\DBAL\DBALException]
  Unknown column type "sbs_crypter" requested. Any Doctrine type that you 
use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can 
get a list of all the known types with 
\Doctrine\DBAL\Types\Type::getTypesMap(). 
If this error occurs during database introspection then you might have 
forgot to register all database types for a Doctrine Type. Use 
AbstractPlatform#registerDoctrineTypeMapping() 
or have your custom types implement Type#getMappedDatabaseTypes(). If the 
type name is empty you might have a problem with the cache or forgot some 
mapping information.



On the other hand, if I put this code on a *compiler pass*, it doesn't work 
either, since *doctrine.dbal.default_connection* isn't known from the 
container yet.

Do you have ideas to make this work both on console and http ?

I'm using Symfony - you guessed it.

Thanks,
Ben

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to