Data protection

This commit is contained in:
Artur Welp
2019-04-28 03:17:00 -03:00
parent ac24b8cc30
commit bacce810a1
2 changed files with 40 additions and 38 deletions
+37 -37
View File
@@ -4,17 +4,17 @@ namespace ORM;
abstract class Entity { abstract class Entity {
const tableName = ''; const _tableName = '';
const connectionName = 'default'; const _connectionName = 'default';
const softdelete = false; const _softdelete = false;
const _ignore = Array( const _ignore = Array(
'classname', 'classname',
'_properties', '_properties',
'_ignore', '_ignore',
'connectionName', '_connectionName',
'timestamps', '_timestamps',
'softdelete', '_softdelete',
'tableName', '_tableName',
'_idPolice' '_idPolice'
); );
const _properties = Array(); const _properties = Array();
@@ -28,7 +28,7 @@ abstract class Entity {
const _idPolice = Array(); const _idPolice = Array();
function __construct() { function __construct() {
if (isset($this->timestamps) && $this->timestamps) { if (isset($this->_timestamps) && $this->_timestamps) {
$this->created_at = date('Y-m-d h:m:s'); $this->created_at = date('Y-m-d h:m:s');
$this->updated_at = date('Y-m-d h:m:s'); $this->updated_at = date('Y-m-d h:m:s');
} }
@@ -71,7 +71,7 @@ abstract class Entity {
private function update() { private function update() {
//First check if the record exist. If not, we might have a idPolice //First check if the record exist. If not, we might have a idPolice
if (!DBInstance::queryOne("SELECT id FROM {" . static::tableName . "} WHERE id = ?", Array($this->id))) { if (!DBInstance::queryOne("SELECT id FROM {" . static::_tableName . "} WHERE id = ?", Array($this->id))) {
$this->create(); $this->create();
return; return;
} }
@@ -92,8 +92,8 @@ abstract class Entity {
$update_string = rtrim($update_string, ', '); $update_string = rtrim($update_string, ', ');
$sql = "UPDATE {" . static::tableName . "} SET $update_string WHERE id = :_update_id"; $sql = "UPDATE {" . static::_tableName . "} SET $update_string WHERE id = :_update_id";
DBInstance::execute($sql, $update_data, static::connectionName); DBInstance::execute($sql, $update_data, static::_connectionName);
} }
private function create() { private function create() {
@@ -101,7 +101,7 @@ abstract class Entity {
//Manual and empty don't need special treatments //Manual and empty don't need special treatments
if (!empty(static::_idPolice) && static::_idPolice['type'] != 'manual') { if (!empty(static::_idPolice) && static::_idPolice['type'] != 'manual') {
//Get the actual max value of the key //Get the actual max value of the key
$maxVal = DBInstance::queryOne("SELECT max(id) as id FROM {" . static::tableName . "}"); $maxVal = DBInstance::queryOne("SELECT max(id) as id FROM {" . static::_tableName . "}");
$maxVal = ($maxVal) ? $maxVal : 0; $maxVal = ($maxVal) ? $maxVal : 0;
// If the next val is smaller than the minumum value // If the next val is smaller than the minumum value
@@ -141,9 +141,9 @@ abstract class Entity {
$first_argument = rtrim($first_argument, ', '); $first_argument = rtrim($first_argument, ', ');
$second_argument = rtrim($second_argument, ', '); $second_argument = rtrim($second_argument, ', ');
$sql = "INSERT INTO {" . static::tableName . "} ($first_argument) VALUES ($second_argument)"; $sql = "INSERT INTO {" . static::_tableName . "} ($first_argument) VALUES ($second_argument)";
DBInstance::execute($sql, $insert_data, static::connectionName); DBInstance::execute($sql, $insert_data, static::_connectionName);
// If there is a id pollice applied, do not get the inserted id // If there is a id pollice applied, do not get the inserted id
if ( empty(static::_idPolice) ){ if ( empty(static::_idPolice) ){
$this->id = DBInstance::lastInsert(); $this->id = DBInstance::lastInsert();
@@ -151,7 +151,7 @@ abstract class Entity {
} }
public function delete() { public function delete() {
if (isset($this->softdelete) && $this->softdelete) { if (isset($this->_softdelete) && $this->_softdelete) {
$this->deleted_at = date('Y-m-d h:m:s'); $this->deleted_at = date('Y-m-d h:m:s');
$this->update(); $this->update();
return; return;
@@ -164,14 +164,14 @@ abstract class Entity {
return; return;
} }
$sql = "SELECT * FROM {" . static::tableName . "} WHERE id = ?"; $sql = "SELECT * FROM {" . static::_tableName . "} WHERE id = ?";
$this->fill(DBInstance::queryPrepare($sql, Array($id), static::connectionName)[0]); $this->fill(DBInstance::queryPrepare($sql, Array($id), static::_connectionName)[0]);
} }
public function purge() { public function purge() {
$sql = "DELETE FROM {" . static::tableName . "} WHERE id = :id"; $sql = "DELETE FROM {" . static::_tableName . "} WHERE id = :id";
DBInstance::execute($sql, Array('id' => $this->id), static::connectionName); DBInstance::execute($sql, Array('id' => $this->id), static::_connectionName);
} }
private function fill($data) { private function fill($data) {
@@ -206,13 +206,13 @@ abstract class Entity {
$limits_sql .= "$key $value "; $limits_sql .= "$key $value ";
} }
if (static::softdelete && !$trashed) { if (static::_softdelete && !$trashed) {
$sql = "SELECT $criteria FROM {" . static::tableName . "} WHERE deleted_at is null $limits_sql"; $sql = "SELECT $criteria FROM {" . static::_tableName . "} WHERE deleted_at is null $limits_sql";
} else { } else {
$sql = "SELECT $criteria FROM {" . static::tableName . "} $limits_sql"; $sql = "SELECT $criteria FROM {" . static::_tableName . "} $limits_sql";
} }
$results = DBInstance::query($sql, Array(), static::connectionName); $results = DBInstance::query($sql, Array(), static::_connectionName);
$objects = Array(); $objects = Array();
foreach ($results as $value) { foreach ($results as $value) {
@@ -258,13 +258,13 @@ abstract class Entity {
$criteria_data[] = $criteria[1]; $criteria_data[] = $criteria[1];
} }
} }
if (static::softdelete && !$trashed) { if (static::_softdelete && !$trashed) {
$sql = "SELECT COUNT(*) FROM {" . static::tableName . "} $criteria_sql AND deleted_at is null"; $sql = "SELECT COUNT(*) FROM {" . static::_tableName . "} $criteria_sql AND deleted_at is null";
} else { } else {
$sql = "SELECT COUNT(*) FROM {" . static::tableName . "} $criteria_sql"; $sql = "SELECT COUNT(*) FROM {" . static::_tableName . "} $criteria_sql";
} }
$results = DBInstance::queryOne($sql, $criteria_data, static::connectionName); $results = DBInstance::queryOne($sql, $criteria_data, static::_connectionName);
return $results->count; return $results->count;
} }
@@ -317,13 +317,13 @@ abstract class Entity {
$limits_sql .= "$key $value "; $limits_sql .= "$key $value ";
} }
if (static::softdelete && !$trashed) { if (static::_softdelete && !$trashed) {
$sql = "SELECT $select_sql FROM {" . static::tableName . "} $criteria_sql AND deleted_at is null $limits_sql"; $sql = "SELECT $select_sql FROM {" . static::_tableName . "} $criteria_sql AND deleted_at is null $limits_sql";
} else { } else {
$sql = "SELECT $select_sql FROM {" . static::tableName . "} $criteria_sql $limits_sql"; $sql = "SELECT $select_sql FROM {" . static::_tableName . "} $criteria_sql $limits_sql";
} }
$results = DBInstance::query($sql, $criteria_data, static::connectionName); $results = DBInstance::query($sql, $criteria_data, static::_connectionName);
$objects = Array(); $objects = Array();
foreach ($results as $value) { foreach ($results as $value) {
@@ -376,13 +376,13 @@ abstract class Entity {
} }
$select_sql = rtrim($select_sql, ', '); $select_sql = rtrim($select_sql, ', ');
if (static::softdelete && !$trashed) { if (static::_softdelete && !$trashed) {
$sql = "SELECT $select_sql FROM {" . static::tableName . "} $criteria_sql AND deleted_at is null $limits_sql"; $sql = "SELECT $select_sql FROM {" . static::_tableName . "} $criteria_sql AND deleted_at is null $limits_sql";
} else { } else {
$sql = "SELECT $select_sql FROM {" . static::tableName . "} $criteria_sql $limits_sql"; $sql = "SELECT $select_sql FROM {" . static::_tableName . "} $criteria_sql $limits_sql";
} }
$results = DBInstance::query($sql, Array(), static::connectionName); $results = DBInstance::query($sql, Array(), static::_connectionName);
$objects = Array(); $objects = Array();
foreach ($results as $value) { foreach ($results as $value) {
@@ -489,7 +489,7 @@ abstract class Entity {
} }
$sql = "SELECT $remote_in_pivot FROM {$pivot_table} WHERE $local_in_pivot = $this->id $limits_sql"; $sql = "SELECT $remote_in_pivot FROM {$pivot_table} WHERE $local_in_pivot = $this->id $limits_sql";
$relations = DBInstance::query($sql, Array(), static::connectionName); $relations = DBInstance::query($sql, Array(), static::_connectionName);
$ids = Array(); $ids = Array();
foreach ($relations as $relation) { foreach ($relations as $relation) {
$ids[] = $relation->$remote_in_pivot; $ids[] = $relation->$remote_in_pivot;
@@ -528,7 +528,7 @@ abstract class Entity {
} }
$sql = "SELECT * FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql"; $sql = "SELECT * FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql";
$relations = DBInstance::query($sql, Array(), static::connectionName); $relations = DBInstance::query($sql, Array(), static::_connectionName);
$objects = Array(); $objects = Array();
if (empty($relations)) { if (empty($relations)) {
+3 -1
View File
@@ -30,7 +30,7 @@ class Estado extends Entity {
class Estado_ extends Entity { class Estado_ extends Entity {
const tableName = 'estado_novo'; const _tableName = 'estado_novo';
const _idPolice = Array( const _idPolice = Array(
'type' => 'nextval', 'type' => 'nextval',
'min' => 500/*, 'min' => 500/*,
@@ -122,6 +122,8 @@ Connections::addConnection($conn);
//var_dump( DBInstance::queryOne('SELECT max(id) as id FROM {estado}')); //var_dump( DBInstance::queryOne('SELECT max(id) as id FROM {estado}'));
var_dump(Estado_::findAll());
$a = new Estado_(); $a = new Estado_();
$a->nome = 'Estado 1'; $a->nome = 'Estado 1';
$a->save(); $a->save();