Implementing idpolices resolve #5
This commit is contained in:
Executable → Regular
+1
@@ -1,2 +1,3 @@
|
|||||||
/netbeans
|
/netbeans
|
||||||
/.idea
|
/.idea
|
||||||
|
/nbproject
|
||||||
+42
-7
@@ -14,12 +14,18 @@ abstract class Entity {
|
|||||||
'connectionName',
|
'connectionName',
|
||||||
'timestamps',
|
'timestamps',
|
||||||
'softdelete',
|
'softdelete',
|
||||||
'tableName'
|
'tableName',
|
||||||
|
'_idPolice'
|
||||||
);
|
);
|
||||||
|
|
||||||
const _properties = Array();
|
const _properties = Array();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* [type] = nextval| manual | empty is the default
|
||||||
|
* [max] = maximum value allowed
|
||||||
|
* [min] = minumul value allowed
|
||||||
|
* [step] = increment by n each id
|
||||||
|
*/
|
||||||
|
const _idPolice = Array();
|
||||||
|
|
||||||
function __construct() {
|
function __construct() {
|
||||||
if (isset($this->timestamps) && $this->timestamps) {
|
if (isset($this->timestamps) && $this->timestamps) {
|
||||||
@@ -35,11 +41,11 @@ abstract class Entity {
|
|||||||
$this->$property = $value;
|
$this->$property = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __toString(){
|
public function __toString() {
|
||||||
if(isset($this->name)){
|
if (isset($this->name)) {
|
||||||
return $this->name;
|
return $this->name;
|
||||||
}
|
}
|
||||||
if(isset($this->nome)){
|
if (isset($this->nome)) {
|
||||||
return $this->nome;
|
return $this->nome;
|
||||||
}
|
}
|
||||||
return static::class;
|
return static::class;
|
||||||
@@ -64,6 +70,12 @@ abstract class Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function update() {
|
private function update() {
|
||||||
|
//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))) {
|
||||||
|
$this->create();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$updatekeys = static::_ignore;
|
$updatekeys = static::_ignore;
|
||||||
$updatekeys[] = 'id';
|
$updatekeys[] = 'id';
|
||||||
$columns = array_diff(array_keys(get_object_vars($this)), $updatekeys);
|
$columns = array_diff(array_keys(get_object_vars($this)), $updatekeys);
|
||||||
@@ -85,6 +97,26 @@ abstract class Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function create() {
|
private function create() {
|
||||||
|
|
||||||
|
|
||||||
|
if (!empty(static::_idPolice) && static::_idPolice['type'] != 'manual') {
|
||||||
|
$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) {
|
||||||
|
$this->id = static::_idPolice['min'];
|
||||||
|
} else {
|
||||||
|
if (isset(static::_idPolice['step'])) {
|
||||||
|
$this->id = $maxVal->id + static::_idPolice['step'];
|
||||||
|
} else {
|
||||||
|
$this->id = $maxVal->id + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isset(static::_idPolice['max']) && $maxVal->id > static::_idPolice['max']) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var_dump($this);
|
||||||
$columns = array_diff(array_keys(get_object_vars($this)), static::_ignore);
|
$columns = array_diff(array_keys(get_object_vars($this)), static::_ignore);
|
||||||
$obj = (array) $this;
|
$obj = (array) $this;
|
||||||
|
|
||||||
@@ -103,8 +135,11 @@ abstract class Entity {
|
|||||||
$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);
|
||||||
$this->id = DBInstance::lastInsert();
|
if ( empty(static::_idPolice) ){
|
||||||
|
$this->id = DBInstance::lastInsert();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete() {
|
public function delete() {
|
||||||
|
|||||||
+16
-1
@@ -28,6 +28,16 @@ class Estado extends Entity {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Estado_ extends Entity {
|
||||||
|
|
||||||
|
const tableName = 'estado_novo';
|
||||||
|
const _idPolice = Array(
|
||||||
|
'type' => 'nextval',
|
||||||
|
'min' => 500/*,
|
||||||
|
'step' => 10*/);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class Municipio extends Entity {
|
class Municipio extends Entity {
|
||||||
|
|
||||||
const tableName = 'basico_geografico_municipios';
|
const tableName = 'basico_geografico_municipios';
|
||||||
@@ -110,4 +120,9 @@ Connections::addConnection($conn);
|
|||||||
//$a->load(1);
|
//$a->load(1);
|
||||||
//$a->estado();
|
//$a->estado();
|
||||||
|
|
||||||
var_dump( DBInstance::queryOne('SELECT * FROM {estado}'));
|
//var_dump( DBInstance::queryOne('SELECT max(id) as id FROM {estado}'));
|
||||||
|
|
||||||
|
$a = new Estado_();
|
||||||
|
$a->nome = 'Estado 1';
|
||||||
|
$a->save();
|
||||||
|
var_dump($a);
|
||||||
Reference in New Issue
Block a user