Iniciando collections

This commit is contained in:
ahwelp
2022-03-23 21:59:33 +00:00
parent cbd8d9ef89
commit 50c4c20ae1
2 changed files with 186 additions and 48 deletions
+55 -48
View File
@@ -40,14 +40,14 @@ abstract class Entity {
}
$this->$property = $value;
}
public function __get($key){
if(!isset($this->$key)){
public function __get($key) {
if (!isset($this->$key)) {
return 0;
}
return $this->toArray()[$key];
}
public function __toString() {
if (isset($this->name)) {
return $this->name;
@@ -58,6 +58,13 @@ abstract class Entity {
return static::class;
}
public function charge($payload) {
foreach (static::_properties as $key => $property) {
$this->$property = $payload[$property];
}
return [];
}
public static function get_properties() {
$properties = Array();
foreach (static::_properties as $propertie) {
@@ -110,13 +117,13 @@ abstract class Entity {
//Get the actual max value of the key
$maxVal = DBInstance::queryOne("SELECT max(id) as id FROM {" . static::_tableName . "}");
$maxVal = ($maxVal) ? $maxVal : 0;
// 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
// 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'];
@@ -130,10 +137,10 @@ abstract class Entity {
return false;
}
}
$columns = array_diff(array_keys(get_object_vars($this)), static::_ignore);
$obj = (array) $this;
$first_argument = '';
$second_argument = '';
$insert_data = Array();
@@ -149,10 +156,10 @@ abstract class Entity {
$second_argument = rtrim($second_argument, ', ');
$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) ){
if (empty(static::_idPolice)) {
$this->id = DBInstance::lastInsert();
}
}
@@ -191,10 +198,10 @@ abstract class Entity {
/**
* Find All
*
*
* This method will load all instances from the class.
* Limits are avaliable for paginations.
*
*
* @param Array $select Columns to select. Array('id', 'name')
* @param Array $limits Array( 'offset'=> 10, 'limit' => 10 )
* @param boolean $trashed Bring trashed elements?
@@ -233,13 +240,13 @@ abstract class Entity {
/**
* Count
*
*
* Count how many records there is on the database
* in relation with this class with the parameters filter
*
*
* @param Array $criterias Criteras for WHERE Array('id' => Array('=', 10) )
* @param boolean $trashed Bring trashed registers?
*
*
*/
public static function count($criterias = Array(), $trashed = false) {
$criteria_sql = "";
@@ -275,7 +282,7 @@ abstract class Entity {
/**
* Find One
*
*
* @param Array/Int $criterias Array('id' => Array('in', Array(10, 20, 30)))
* @param Array $select Array(id, fullname)
* @param Boolan $trashed true, false
@@ -346,12 +353,12 @@ abstract class Entity {
/**
* Find Many
*
*
* @param Array $criterias Description
* @param Array $select Description
* @param Array $limits Description
* @param boolean $trashed Description
*
*
*/
public static function findMany($criterias = Array(), $select = Array('*'), $limits = Array(), $trashed = false) {
$select_sql = "";
@@ -405,23 +412,23 @@ abstract class Entity {
/**
* Has One Local
*
*
* Defines that, this object has a child an only one,
* object in other class.
*
*
* Table user = (id, name)
* Table phone = (id, number, userid)
*
*
* This relation will be created on the User class
* making reference to the Phone class
*
*
* @ctag $this->hasOne('foreign_object', 'field_in_remote');
*
* @param Object $foreign_object Class instance from the remote object
* @param (int, string) $field_in_remote Field in local object matchin the remote id
*
*
* @return Object Instance of the remote class
*
*
*/
protected function hasOne($foreign_object, $field_in_remote) {
$obj = new $foreign_object;
@@ -430,20 +437,20 @@ abstract class Entity {
/**
* Has Many
*
*
* Defines that, this object has many instances of other Object
*
*
* Table Post = (id, name, content)
* Table Comment = (id, name, content, postid)
*
*
* This relation will be created on the Post class
* Making reference to the Comment class
*
*
* @ctag $this->hasMany('foreign_object', 'field_in_foreign');
*
* @param type $foreign_object Instance of a remote class
* @param (int, string) $field_in_foreign The field to match the local id
*
*
*/
protected function hasMany($foreign_object, $field_in_foreign) {
$obj = new $foreign_object;
@@ -452,22 +459,22 @@ abstract class Entity {
/**
* Belongs To
*
*
* Defines that, this object is part of an other class.
* The local field must match the id of an other class.
*
*
* Table Post = (id, name, content)
* Table Comment = (id, name, content, postid)
*
* Table Comment = (id, name, content, postid)
*
* This relation will be created on the comment class
* Making reference to the post class
*
* @ctag $this->belongsTo('foreign_object', 'local_field');
* @ctag $this->belongsTo('foreign_object', 'local_field', 'id');
*
*
* @param Object $foreign_object Instance of a remote class
* @param (int, String) $local_field Remote field relate to local Object
* @param string $remote_field
* @param string $remote_field
*/
protected function belongsTo($foreign_object, $local_field, $remote_field = 'id') {
$obj = new $foreign_object;
@@ -476,20 +483,20 @@ abstract class Entity {
/**
* Belongs to Many
*
*
* Defines that this object is related to many other instances
* of other classes throw a pivot table.
*
*
* Table Post = (id, name, content)
* Table Tag = (id, name)
* Table Post_Tag = (id, postid, tagid)
*
* Table Post_Tag = (id, postid, tagid)
*
* This relation will be created in both classes
*
* @ctag $this->belongsToMany('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot');
*
* @ctag $this->belongsToMany('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot');
* @ctag $this->belongsToMany('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot', $remote_filter = Array());
* @ctag $this->belongsToMany('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot', $remote_filter = Array(), $remote_limit = Array());
*
*
* @param Object $foreign_object Instance of the remote class
* @param string $pivot_table Name of the pivot table
* @param int $local_in_pivot Name of field on the pivot in relation of the local class
@@ -519,21 +526,21 @@ abstract class Entity {
/**
* Belongs to Many Extended
*
*
* Defines that this object is related to many other instances
* of other classes throw a pivot table.
* This relation will bring the pivot table with the elements inside
*
*
* Table Post = (id, name, content)
* Table Tag = (id, name)
* Table Post_Tag = (id, postid, tagid)
*
* @ctag $this->belongsToManyExtended('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot');
* Table Post_Tag = (id, postid, tagid)
*
* @ctag $this->belongsToManyExtended('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot');
* @ctag $this->belongsToManyExtended('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot', $remote_filter = Array());
* @ctag $this->belongsToManyExtended('foreign_object', 'pivot_table', 'local_in_pivot', 'remote_in_pivot', $remote_filter = Array(), $pivot_limits = Array());
*
* This relation will be created in both classes
*
*
* @param Object $foreign_object Instance of the remote class
* @param string $pivot_table Name of the pivot table
* @param int $local_in_pivot Name of field on the pivot in relation of the local class