Enable MySQL encryption for models

Hello everybody,

i would like save encrypted data in a mysql database. is there any way to activate this in a yii data model? That i can use $model->save() to save the data or must i do this with query builder?

Thanks for inspirations…

You can do it in many ways:

  1. Using mysql native encryption, thus transparenly for application (I would choose this way).
  2. Overriding afterFind() and beforeSave() methods.
  3. Responding to events corresponding to mentioned methods.

Hi BartQ76,

you meen something like

INSERT INTO your_table (username,email) VALUES ('$username', AES_ENCRYPT('$email','".SALT."'));

But then i can’t use $model->save(), right? Then i must create a mysql statement with the query builder function, right?

No. I rather think of https://www.mysql.com/products/enterprise/tde.html

Data is encrypted automatically, in real time, prior to writing to storage and decrypted when read from storage.

1 Like

We don’t use the mysql enterprise edition. So i must find another way. Thank you for your suggestion.

Hello, i think what youre looking for probably have something to do with Yii’s security class. Try and check out

Yii::$app->security->(the functions on the class here);

depending on your usage of what youre trying to encrypt, most of the encryption is here.

You got it on MariaDB also: https://mariadb.com/kb/en/data-at-rest-encryption-overview/#overview