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(); }