From ac24b8cc3029c5721b42f20fa05a5bacd3d9d447 Mon Sep 17 00:00:00 2001 From: Artur Welp Date: Thu, 25 Apr 2019 04:14:04 -0300 Subject: [PATCH] Documentation on #5 --- src/ORM/Entity.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/ORM/Entity.php b/src/ORM/Entity.php index b9ebf82..cf3b7f8 100755 --- a/src/ORM/Entity.php +++ b/src/ORM/Entity.php @@ -97,26 +97,33 @@ abstract class Entity { } private function create() { - - + //First, let's verify id polices + //Manual and empty don't need special treatments if (!empty(static::_idPolice) && static::_idPolice['type'] != 'manual') { + //Get the actual max value of the key $maxVal = DBInstance::queryOne("SELECT max(id) as id FROM {" . static::tableName . "}"); $maxVal = ($maxVal) ? $maxVal : 0; - if (isset(static::_idPolice['min']) && static::_idPolice['min'] > $maxVal->id) { + // If the next val is smaller than the minumum value + if (isset(static::_idPolice['min']) && static::_idPolice['min'] > $maxVal->id+1) { + // The id value shpuld assume the min value $this->id = static::_idPolice['min']; } else { + // The id is bigger than the min or min is not set if (isset(static::_idPolice['step'])) { + //If sted is defined, increment by step multiples $this->id = $maxVal->id + static::_idPolice['step']; } else { $this->id = $maxVal->id + 1; } } + //If the id is bigger than the max value. There is problems if (isset(static::_idPolice['max']) && $maxVal->id > static::_idPolice['max']) { + //Do not store the register return false; } } - var_dump($this); + $columns = array_diff(array_keys(get_object_vars($this)), static::_ignore); $obj = (array) $this; @@ -137,6 +144,7 @@ abstract class Entity { $sql = "INSERT INTO {" . static::tableName . "} ($first_argument) VALUES ($second_argument)"; DBInstance::execute($sql, $insert_data, static::connectionName); + // If there is a id pollice applied, do not get the inserted id if ( empty(static::_idPolice) ){ $this->id = DBInstance::lastInsert(); }