Doctrine2自定义类型不返回数据

时间:2013-11-27 21:12:09

标签: symfony doctrine-orm custom-type

因为我想到我可以创建加密的自定义类型,所以我可以创建加密的自定义类型,以便在该类型中完成数据的加密和解密,以便我不需要将这些函数称为evrytime,通常是为了让我生活更轻松,但它变成了生活地狱:/

我的自定义类型是按照我的意图在数据库中插入加密数据,但问题是当我需要检索数据时,这里是打印从数据库中保存已检索数据的对象

    Uapi\CoreBundle\Entity\Request Object
(
    [id:Uapi\CoreBundle\Entity\Request:private] => 2
    [clientIPAdress:Uapi\CoreBundle\Entity\Request:private] => 
    [clientHost:Uapi\CoreBundle\Entity\Request:private] => 
    [clientISP:Uapi\CoreBundle\Entity\Request:private] => 
    [clientOS:Uapi\CoreBundle\Entity\Request:private] => 
    [clientOSVersion:Uapi\CoreBundle\Entity\Request:private] => 
    [clientBrowser:Uapi\CoreBundle\Entity\Request:private] => 
    [clientBrowserVersion:Uapi\CoreBundle\Entity\Request:private] => 
    [clientLanguage:Uapi\CoreBundle\Entity\Request:private] => 
    [clientCountry:Uapi\CoreBundle\Entity\Request:private] => 
    [clientCity:Uapi\CoreBundle\Entity\Request:private] => 
    [possibleProxy:Uapi\CoreBundle\Entity\Request:private] => 
    [proxyIPAdress:Uapi\CoreBundle\Entity\Request:private] => 
    [torExitNode:Uapi\CoreBundle\Entity\Request:private] => 
    [maliciousInput:Uapi\CoreBundle\Entity\Request:private] => 
    [maliciousInputLog:Uapi\CoreBundle\Entity\Request:private] => 
    [serverIPAdress:Uapi\CoreBundle\Entity\Request:private] => 
    [serverPort:Uapi\CoreBundle\Entity\Request:private] => 
    [requestMethod:Uapi\CoreBundle\Entity\Request:private] => 
    [requestTimeFloat:Uapi\CoreBundle\Entity\Request:private] => P�a�;
)

这是我的加密类型类定义

    use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Uapi\CoreBundle\Provider\EncryptionProvider;

class EncryptedType extends Type {

    const ENCRYPTED = 'encrypted';

    public function getName()
    {
        return self::ENCRYPTED;
    }

    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) {
        return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform) {
        return ($value === null)? null : base64_encode(EncryptionProvider::getCrypter()->encrypt($value));
    }

    public function convertToPHPValue($value, AbstractPlatform $platform) {
        return ($value === null)? null : base64_decode(EncryptionProvider::getCrypter()->decrypt($value));
    }
}

我确实在核心包类

中的引导函数中注册了它
 $connection = $this->container->get('doctrine')->getConnection();
        if(!Type::hasType('encrypted'))
        {
            Type::addType('encrypted', 'Uapi\CoreBundle\System\DBALType\EncryptedType');
            $connection->getDatabasePlatform()->registerDoctrineTypeMapping('encrypted', 'encrypted');
        }

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题,这不是一般问题,但在这里解释不好。我需要首先定义一个EncryptionProvider,它是我的类,它实现了PHPSecLib \ AES。这是正确的

public function boot()
{
    new EncryptionInit();
    EncryptionProvider::setCrypter(EncryptionProvider::AES, $this->container->getParameter('uapi_core.security_key'), $x = array());

    $connection = $this->container->get('doctrine')->getConnection();
    if(!Type::hasType('encrypted'))
    {
        Type::addType('encrypted', 'Uapi\CoreBundle\System\DBALType\EncryptedType');
        $connection->getDatabasePlatform()->registerDoctrineTypeMapping('encrypted', 'encrypted');
    }


 }