Bhavin Thummar
Bhavin Thummar

Reputation: 1293

Yii2: Using mysql AES_ENCRYPT while inserting into database

I have got the following error :

AES_ENCRYPT method not found.

Please help me with using this SQL method in YII2.

public function beforeSave($insert) {
        if (!parent::beforeSave($insert)) {
            return false;
        }
        if($insert) {
            $this->created_at = date('Y-m-d H:i:s');
        }
        $this->updated_at = date('Y-m-d H:i:s');
        $this->name = AES_ENCRYPT("'.$this->name.'", "SECERT KEY");
        return true;
    }

Upvotes: 2

Views: 509

Answers (1)

Muhammad Omer Aslam
Muhammad Omer Aslam

Reputation: 23748

Because calling it like

$this->name = AES_ENCRYPT("'.$this->name.'", "SECERT KEY");

will assume that it is a php method and would search in php functions available whereas, you need to run it like you do for CONCAT, SUM or any other MYSQL functions.

You should use it with the \yii\db\Expression() in the following way

$enc = new \yii\db\Expression('AES_ENCRYPT("'.$this->name.'","SECERT KEY")');
$this->name=$enc;

or simplifying it

$this->name=new \yii\db\Expression('AES_ENCRYPT("'.$this->name.'","SECERT KEY")');

Hope it helps you out.

Upvotes: 2

Related Questions