nlt_codilog / database-change-log_fork
A PHP class to log all changes of table data from raw SQL or with PDO statement
V1.0.0
2021-10-19 18:45 UTC
Requires
- php: >=5.3
- greenlion/php-sql-parser: 4.3.0
README
Note: This repository is a fork of https://github.com/moledet/database-change-log
Installation
###Download
From Git
###Clone
git clone https://github.com/nltCodilog/database-change-log_fork.git
###Composer
php composer.phar require nlt_codilog/database-change-log_fork
or add to yours composer.json see the documentation.
{ "repositories": [ { "url": "https://github.com/nltCodilog/database-change-log_fork.git", "type": "git" } ], "require": { "nltCodilog/database-change-log": "main" } }
###Dependency This class depends on PHP-SQL-Parser.
Usage
###Config You must config a database connection.
$config = array( 'database'=>'mysql', 'host'=>'localhost', 'port'=>3306, 'dbname'=>'test', 'charset'=>'utf8', 'user'=>'admin', 'password'=>'secret' ); DatabaseChangeLog::getInstance()->setConnection($config);
May config current user id (default 0), system name(default CRM) or list of tables|columns|actions that need log. If not config the tables list - all tables changes will be logged.
DatabaseChangeLog::getInstance()->setUserId(7); DatabaseChangeLog::getInstance()->setSystemName('API'); $config = [ 'user'=>[ 'insert'=>['login','name','password'] 'delete'=>'all', 'update'=>['login','name'] ], 'customers'=>'all', ]; DatabaseChangeLog::getInstance()->setLogTablesConfig($config);
###How to use Need put call of log sql before run. You may override framework or ORM connection to run it before query.
$sql = "UPDATE user SET password='secret' WHERE id=7;"; DatabaseChangeLog::getInstance()->log($sql); $framework->getConnection()->runSQL($sql);
Or PDO:
$query = 'UPDATE users SET bonus = bonus + ? WHERE id = ?'; $stmt = $pdo->prepare($query); foreach ($data as $id => $bonus) { DatabaseChangeLog::getInstance()->log($query,[$bonus,$id]); $stmt->execute([$bonus,$id]); }
###Result In table data_change_log will be save the log of changes.