2013-11-27 72 views
0

iam過去兩天感到後悔,因爲我想到我可以創建加密的自定義類型,以便在該類型中完成數據的加密和解密,這樣我就不需要調用這些函數evrytime,一般爲了讓我的生活更輕鬆,但它變成了生活地獄:/Doctrine2自定義類型不返回數據

我的自定義類型是在數據庫中插入加密數據,因爲我打算但問題是當我需要檢索數據時,這裏是打印的對象從數據庫中保存已收回的數據

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

這裏有什麼問題嗎?什麼不起作用?任何例外?你已經嘗試過了什麼? – nifr

+0

剛剛閱讀問題的人,iam在這邊,iam沒有從數據庫中獲取數據,因爲您可以從打印對象中看到,他只是有空屬性 – user3035443

+2

閱讀什麼問題「* man *」?我在你的非常詳細的「*問題*」中的任何地方都沒有看到問號...... btw自定義類型通常添加在DoctrineBundle的配置部分,在你的app/config/config中的doctrine.dbal.types下.yml'或者一些文件被導入到那裏,持有'doctrine'配置...參見[配置參考](http://symfony.com/doc/current/reference/configuration/doctrine.html)。 – nifr

回答

0

我沒有解決這個問題對我自己的,它不是一般的問題,但解釋生病需要這兒過得首先要定義一個EncryptionProvider啓動功能中進行註冊這是我的班級,實施PHPSecLib \ AES.So這是正確的

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'); 
    } 


}