2016-04-16 2 views
0

私は現実のセッターによってフィールドが変更されていて、フィールドが正しく更新されています。save()保存された値は、返されたものではなく、私のセッターの中間で計算された値です(以下のコードを参照してください)。CakePHP3.2:save()は、セッターによってフィールドが変わっても変わっていません

unlock_keyは、varchar(255)utf8_general_ciフィールドです。

モデルは次のとおりです。

class Agtheme extends Entity { 

/** 
* Fields that can be mass assigned using newEntity() or patchEntity(). 
* 
* @var array 
*/ 
    protected $_accessible = [ 
     ... 
     'unlock_key' => true, 
     ... 
    ]; 

    protected function _setUnlockKey($data) { 

     if (empty($data)) { 
      return ''; 
     } 

     $AESEncryption = Configure::read('AESEncryption'); 

     if(32 !== strlen($AESEncryption['password'])) $secret = hash('SHA256', $AESEncryption['password'], true); 

     $padding = 16 - (strlen($data) % 16); 
     $data .= str_repeat(chr($padding), $padding); // !!!! Here is the value I find in my DB !!!! 

     $encrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $secret, $data, MCRYPT_MODE_CBC, str_repeat("\0", 16)); 
     return base64_encode($encrypt); 
    } 
    .... 
} 

そして、私のコードは次のとおりです。

$agTheme->unlock_key = 'not encrypted string'; 

debug($agTheme); // The output is below 

if (!$this->Agthemes->save($agTheme)) { 
    debug($agTheme); // I don't come here. 
    die('Error'); 
} 
die(); 

debug($agTheme)出力は次のとおりです。

object(App\Model\Entity\Agtheme) { 

    'id' => (int) 1, 
    ...., 
    'unlock_key' => 'QCXW7ksUpdmH6QfVua62DUDbHjLcvUF38MAneG9KGKo=', // this value is the one I want to see in my DB 
    ...., 
    '[new]' => false, 
    '[accessible]' => [ 
     ...., 
     'unlock_key' => true, 
     ...., 
    ], 
    '[dirty]' => [ 
     'unlock_key' => true 
    ], 
    '[original]' => [ 
     'unlock_key' => null 
    ], 
    '[virtual]' => [], 
    '[errors]' => [], 
    '[invalid]' => [], 
    '[repository]' => 'Agthemes' 

} 

のでsave()た後、私はパディング文字でnot encrypted stringを踏襲していますDB。

なぜですか?

答えて

0

まずお読みください:

ミューテータメソッドは、常にプロパティに格納されるべき値を返す必要があります。また、ミューテータを使用して、他の計算されたプロパティを設定することもできます。このとき、CakePHPは無限ループのmutatorメソッドを防ぐことができないので、ループを導入しないように注意してください。ミューテータを使用すると、プロパティが設定されたときにプロパティを変換したり、計算されたデータを作成できます。オブジェクト記法を使用してプロパティを読み込んだ場合、またはget()およびset()を使用してプロパティを読み取った場合は、Mutatorsおよびアクセサが適用されます。

あなたは今希望が役立ちます。この方法を試すことができます。

function _setUnlockKey($unlock_key) { 

    if (empty($unlock_key)) { 
    return ''; 
    } 

    $AESEncryption = Configure::read('AESEncryption'); 

    if(32 !== strlen($AESEncryption['password'])) $secret = hash('SHA256', $AESEncryption['password'], true); 

    $padding = 16 - (strlen($unlock_key) % 16); 
    $unlock_key .= str_repeat(chr($padding), $padding); 

    // $encrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $secret, $unlock_key, MCRYPT_MODE_CBC, str_repeat("\0", 16)); 


    $encripton = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $secret, $unlock_key, MCRYPT_MODE_CBC, str_repeat("\0", 16)); 

    $unlock_key = base64_encode($encripton); 

    return $unlock_key; 

    } 

お読みください:&ミューテータは

http://manual.cakephp.neoboots.com/3.0/en/orm/entities.html

アクセサ
関連する問題