|
@@ -27,11 +27,19 @@ abstract class Entity {
|
|
|
*/
|
|
*/
|
|
|
const _idPolice = Array();
|
|
const _idPolice = Array();
|
|
|
|
|
|
|
|
- function __construct() {
|
|
|
|
|
|
|
+ function __construct($assignment = '') {
|
|
|
|
|
+
|
|
|
|
|
+ @$this->_ignore = array_merge($this->_ignore, self::_ignore);
|
|
|
|
|
+
|
|
|
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');
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // Mass Assign
|
|
|
|
|
+ if(is_array($assignment)){
|
|
|
|
|
+ $this->charge($assignment);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function __set($property, $value) {
|
|
public function __set($property, $value) {
|
|
@@ -59,7 +67,16 @@ abstract class Entity {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function charge($payload) {
|
|
public function charge($payload) {
|
|
|
- foreach (static::_properties as $key => $property) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ $elements = array_diff(static::_properties, self::_ignore, $this->_ignore);
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($elements as $key => $property) {
|
|
|
|
|
+ if ($property == 'id' && !isset($payload[$property])) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ if(empty($payload[$property])){
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
$this->$property = $payload[$property];
|
|
$this->$property = $payload[$property];
|
|
|
}
|
|
}
|
|
|
return [];
|
|
return [];
|
|
@@ -99,6 +116,10 @@ abstract class Entity {
|
|
|
$update_data = Array();
|
|
$update_data = Array();
|
|
|
$update_data['_update_id'] = $this->id;
|
|
$update_data['_update_id'] = $this->id;
|
|
|
|
|
|
|
|
|
|
+ if (isset($this->_timestamps) && $this->_timestamps) {
|
|
|
|
|
+ $this->updated_at = date('Y-m-d h:m:s');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
foreach ($columns as $column) {
|
|
foreach ($columns as $column) {
|
|
|
$update_string .= $column . " = :$column, ";
|
|
$update_string .= $column . " = :$column, ";
|
|
|
$update_data[$column] = $obj[$column];
|
|
$update_data[$column] = $obj[$column];
|
|
@@ -119,7 +140,7 @@ abstract class Entity {
|
|
|
$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
|
|
|
- if (isset(static::_idPolice['min']) && static::_idPolice['min'] > $maxVal->id+1) {
|
|
|
|
|
|
|
+ if (isset(static::_idPolice['min']) && static::_idPolice['min'] > $maxVal->id + 1) {
|
|
|
// The id value shpuld assume the min value
|
|
// The id value shpuld assume the min value
|
|
|
$this->id = static::_idPolice['min'];
|
|
$this->id = static::_idPolice['min'];
|
|
|
} else {
|
|
} else {
|
|
@@ -164,14 +185,6 @@ abstract class Entity {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public function delete() {
|
|
|
|
|
- if (isset($this->_softdelete) && $this->_softdelete) {
|
|
|
|
|
- $this->deleted_at = date('Y-m-d h:m:s');
|
|
|
|
|
- $this->update();
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- $this->purge();
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
public function load($id = false) {
|
|
public function load($id = false) {
|
|
|
if (!$id) {
|
|
if (!$id) {
|
|
@@ -183,11 +196,29 @@ abstract class Entity {
|
|
|
$this->fill(DBInstance::queryPrepare($sql, Array($id), static::_connectionName)[0]);
|
|
$this->fill(DBInstance::queryPrepare($sql, Array($id), static::_connectionName)[0]);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public function delete() {
|
|
|
|
|
+ if (isset($this->_softdelete) && $this->_softdelete) {
|
|
|
|
|
+ $this->deleted_at = date('Y-m-d h:m:s');
|
|
|
|
|
+ $this->update();
|
|
|
|
|
+ }else{
|
|
|
|
|
+ $this->purge();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
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);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public function restore(){
|
|
|
|
|
+ if (isset($this->_softdelete) && $this->_softdelete) {
|
|
|
|
|
+ $this->deleted_at = null;
|
|
|
|
|
+ $this->update();
|
|
|
|
|
+ }else{
|
|
|
|
|
+ throw new \Exception('Not soft deleteble');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private function fill($data) {
|
|
private function fill($data) {
|
|
|
$data = (array) $data;
|
|
$data = (array) $data;
|
|
|
|
|
|
|
@@ -206,7 +237,7 @@ abstract class Entity {
|
|
|
* @param Array $limits Array( 'offset'=> 10, 'limit' => 10 )
|
|
* @param Array $limits Array( 'offset'=> 10, 'limit' => 10 )
|
|
|
* @param boolean $trashed Bring trashed elements?
|
|
* @param boolean $trashed Bring trashed elements?
|
|
|
*/
|
|
*/
|
|
|
- public static function findAll($select = Array('*'), $limits = Array(), $trashed = false) {
|
|
|
|
|
|
|
+ public static function findAll($select = Array('*'), $limits = Array(), $trashed = false) : Collection {
|
|
|
$criteria = '';
|
|
$criteria = '';
|
|
|
$limits_sql = '';
|
|
$limits_sql = '';
|
|
|
|
|
|
|
@@ -225,15 +256,15 @@ abstract class Entity {
|
|
|
} else {
|
|
} else {
|
|
|
$sql = "SELECT $criteria FROM {" . static::_tableName . "} $limits_sql";
|
|
$sql = "SELECT $criteria FROM {" . static::_tableName . "} $limits_sql";
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+ error_log($sql);
|
|
|
$results = DBInstance::query($sql, Array(), static::_connectionName);
|
|
$results = DBInstance::query($sql, Array(), static::_connectionName);
|
|
|
- $objects = Array();
|
|
|
|
|
|
|
+ $objects = new Collection;
|
|
|
|
|
|
|
|
foreach ($results as $value) {
|
|
foreach ($results as $value) {
|
|
|
$static = static::class;
|
|
$static = static::class;
|
|
|
$object = new $static;
|
|
$object = new $static;
|
|
|
$object->fill($value);
|
|
$object->fill($value);
|
|
|
- $objects[] = $object;
|
|
|
|
|
|
|
+ $objects->addItem($object);
|
|
|
}
|
|
}
|
|
|
return $objects;
|
|
return $objects;
|
|
|
}
|
|
}
|
|
@@ -288,6 +319,10 @@ abstract class Entity {
|
|
|
* @param Boolan $trashed true, false
|
|
* @param Boolan $trashed true, false
|
|
|
*/
|
|
*/
|
|
|
public static function findOne($criterias = Array(), $select = Array('*'), $trashed = false) {
|
|
public static function findOne($criterias = Array(), $select = Array('*'), $trashed = false) {
|
|
|
|
|
+ if(empty($criterias)){
|
|
|
|
|
+ $criterias = Array(1 => ['=', 1]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
$select_sql = "";
|
|
$select_sql = "";
|
|
|
$criteria_sql = "";
|
|
$criteria_sql = "";
|
|
|
$limits_sql = "";
|
|
$limits_sql = "";
|
|
@@ -302,8 +337,10 @@ abstract class Entity {
|
|
|
foreach ($criterias as $key => $criteria) {
|
|
foreach ($criterias as $key => $criteria) {
|
|
|
if ($criteria_sql != "") {
|
|
if ($criteria_sql != "") {
|
|
|
$criteria_sql .= " AND ";
|
|
$criteria_sql .= " AND ";
|
|
|
- } else {
|
|
|
|
|
|
|
+ } else if ($criteria_sql == "") {
|
|
|
$criteria_sql .= " WHERE ";
|
|
$criteria_sql .= " WHERE ";
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $criteria_sql .= " ";
|
|
|
}
|
|
}
|
|
|
if (is_array($criteria[1])) {
|
|
if (is_array($criteria[1])) {
|
|
|
$crit_temp = '';
|
|
$crit_temp = '';
|
|
@@ -335,19 +372,19 @@ abstract class Entity {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$results = DBInstance::query($sql, $criteria_data, static::_connectionName);
|
|
$results = DBInstance::query($sql, $criteria_data, static::_connectionName);
|
|
|
- $objects = Array();
|
|
|
|
|
|
|
+ $objects = new Collection();
|
|
|
|
|
|
|
|
foreach ($results as $value) {
|
|
foreach ($results as $value) {
|
|
|
$class = static::class;
|
|
$class = static::class;
|
|
|
$object = new $class;
|
|
$object = new $class;
|
|
|
$object->fill($value);
|
|
$object->fill($value);
|
|
|
- $objects[] = $object;
|
|
|
|
|
|
|
+ $objects->addItem($object);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
if (empty($objects)) {
|
|
if (empty($objects)) {
|
|
|
- //return false; //new $this->classname;
|
|
|
|
|
- $className = static::class;
|
|
|
|
|
- return new $className;
|
|
|
|
|
|
|
+ return false; //new $this->classname;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
return $objects[0];
|
|
return $objects[0];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -368,14 +405,24 @@ abstract class Entity {
|
|
|
foreach ($limits as $key => $value) {
|
|
foreach ($limits as $key => $value) {
|
|
|
$limits_sql .= "$key $value ";
|
|
$limits_sql .= "$key $value ";
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- foreach ($criterias as $key => $criteria) {
|
|
|
|
|
- if ($criteria_sql != "") {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($criterias as $key => $criteria) {
|
|
|
|
|
+ if ($criteria_sql != "" && substr($criteria[0], 0, 2) == 'OR') {
|
|
|
|
|
+ $criteria_sql .= " ";
|
|
|
|
|
+ } else if ($criteria_sql != "" ) {
|
|
|
|
|
+ $criteria_sql .= " ";
|
|
|
|
|
+ } else if ($criteria_sql != "" ) {
|
|
|
$criteria_sql .= " AND ";
|
|
$criteria_sql .= " AND ";
|
|
|
- } else {
|
|
|
|
|
|
|
+ } else if ($criteria_sql == "") {
|
|
|
$criteria_sql .= " WHERE ";
|
|
$criteria_sql .= " WHERE ";
|
|
|
|
|
+ } else {
|
|
|
|
|
+ $criteria_sql .= " ";
|
|
|
}
|
|
}
|
|
|
- if (is_string($criteria[1])) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (substr($criteria[0], 0, 2) == "OR") {
|
|
|
|
|
+ $criteria[0] = str_replace('OR', '', $criteria[0]);
|
|
|
|
|
+ $criteria_sql .= "OR $key::text $criteria[0] ('%$criteria[1]%')";
|
|
|
|
|
+ } else if (is_string($criteria[1])) {
|
|
|
$criteria_sql .= "$key $criteria[0] '$criteria[1]'";
|
|
$criteria_sql .= "$key $criteria[0] '$criteria[1]'";
|
|
|
} else if (is_array($criteria[1])) {
|
|
} else if (is_array($criteria[1])) {
|
|
|
$criteria_sql .= "$key $criteria[0] (" . implode(', ', $criteria[1]) . ")";
|
|
$criteria_sql .= "$key $criteria[0] (" . implode(', ', $criteria[1]) . ")";
|
|
@@ -384,9 +431,12 @@ abstract class Entity {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ $criteria_sql = str_replace('WHERE OR', 'WHERE ', $criteria_sql);
|
|
|
|
|
+
|
|
|
foreach ($select as $value) {
|
|
foreach ($select as $value) {
|
|
|
$select_sql .= $value . ', ';
|
|
$select_sql .= $value . ', ';
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
$select_sql = rtrim($select_sql, ', ');
|
|
$select_sql = rtrim($select_sql, ', ');
|
|
|
|
|
|
|
|
if (static::_softdelete && !$trashed) {
|
|
if (static::_softdelete && !$trashed) {
|
|
@@ -396,13 +446,13 @@ abstract class Entity {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$results = DBInstance::query($sql, Array(), static::_connectionName);
|
|
$results = DBInstance::query($sql, Array(), static::_connectionName);
|
|
|
- $objects = Array();
|
|
|
|
|
|
|
+ $objects = new Collection();
|
|
|
|
|
|
|
|
foreach ($results as $value) {
|
|
foreach ($results as $value) {
|
|
|
$class = static::class;
|
|
$class = static::class;
|
|
|
$object = new $class;
|
|
$object = new $class;
|
|
|
$object->fill($value);
|
|
$object->fill($value);
|
|
|
- $objects[] = $object;
|
|
|
|
|
|
|
+ $objects->addItem($object);
|
|
|
}
|
|
}
|
|
|
if (empty($objects)) {
|
|
if (empty($objects)) {
|
|
|
return Array();
|
|
return Array();
|
|
@@ -431,8 +481,7 @@ abstract class Entity {
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
protected function hasOne($foreign_object, $field_in_remote) {
|
|
protected function hasOne($foreign_object, $field_in_remote) {
|
|
|
- $obj = new $foreign_object;
|
|
|
|
|
- return $obj->findOne(Array($field_in_remote => Array('=', $this->id)));
|
|
|
|
|
|
|
+ return new HasOne($foreign_object, $field_in_remote, $this);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -453,8 +502,7 @@ abstract class Entity {
|
|
|
*
|
|
*
|
|
|
*/
|
|
*/
|
|
|
protected function hasMany($foreign_object, $field_in_foreign) {
|
|
protected function hasMany($foreign_object, $field_in_foreign) {
|
|
|
- $obj = new $foreign_object;
|
|
|
|
|
- return $obj->findMany(Array($field_in_foreign => Array('=', $this->id)));
|
|
|
|
|
|
|
+ return new HasMany($foreign_object, $field_in_foreign, $this);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -476,9 +524,8 @@ abstract class Entity {
|
|
|
* @param (int, String) $local_field Remote field relate to local Object
|
|
* @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;
|
|
|
|
|
- return $obj->findOne(Array($remote_field => Array('=', $this->$local_field)));
|
|
|
|
|
|
|
+ protected function belongsTo($foreignObject, $localField, $remoteField = 'id') {
|
|
|
|
|
+ return new BelongsTo($foreignObject, $localField, $remoteField, $this);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -504,24 +551,8 @@ abstract class Entity {
|
|
|
* @param Array $remote_filter Filters to the remote Array('id', Array('>', 50) )
|
|
* @param Array $remote_filter Filters to the remote Array('id', Array('>', 50) )
|
|
|
* @param Array $remote_limit Array( 'offset'=> 10, 'limit' => 10 )
|
|
* @param Array $remote_limit Array( 'offset'=> 10, 'limit' => 10 )
|
|
|
*/
|
|
*/
|
|
|
- protected function belongsToMany($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $remote_limit = Array()) {
|
|
|
|
|
- $obj = new $foreign_object;
|
|
|
|
|
- $limits_sql = '';
|
|
|
|
|
-
|
|
|
|
|
- foreach ($remote_limit as $key => $value) {
|
|
|
|
|
- $limits_sql .= "$key $value ";
|
|
|
|
|
- }
|
|
|
|
|
- $sql = "SELECT $remote_in_pivot FROM {$pivot_table} WHERE $local_in_pivot = $this->id $limits_sql";
|
|
|
|
|
-
|
|
|
|
|
- $relations = DBInstance::query($sql, Array(), static::_connectionName);
|
|
|
|
|
- $ids = Array();
|
|
|
|
|
- foreach ($relations as $relation) {
|
|
|
|
|
- $ids[] = $relation->$remote_in_pivot;
|
|
|
|
|
- }
|
|
|
|
|
- if (empty($ids)) {
|
|
|
|
|
- return Array();
|
|
|
|
|
- }
|
|
|
|
|
- return $obj->findMany(array_merge(Array('id' => Array('IN', $ids)), $remote_filter), Array('*'), $remote_limit);
|
|
|
|
|
|
|
+ protected function belongsToMany($foreignObject, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $remote_limit = Array()) {
|
|
|
|
|
+ return new BelongsToMany($this, $foreignObject, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter, $remote_limit);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -548,28 +579,32 @@ abstract class Entity {
|
|
|
* @param Array $remote_filter Filters to the remote Array('id', Array('>', 50) )
|
|
* @param Array $remote_filter Filters to the remote Array('id', Array('>', 50) )
|
|
|
* @param Array $pivot_limits Array( 'offset'=> 10, 'limit' => 10 )
|
|
* @param Array $pivot_limits Array( 'offset'=> 10, 'limit' => 10 )
|
|
|
*/
|
|
*/
|
|
|
- protected function belongsToManyExtended($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $pivot_limits = Array()) {
|
|
|
|
|
- $obj = new $foreign_object;
|
|
|
|
|
- $limits_sql = '';
|
|
|
|
|
- foreach ($pivot_limits as $key => $value) {
|
|
|
|
|
- $limits_sql .= "$key $value ";
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- $sql = "SELECT * FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql";
|
|
|
|
|
- $relations = DBInstance::query($sql, Array(), static::_connectionName);
|
|
|
|
|
- $objects = Array();
|
|
|
|
|
|
|
+ protected function belongsToManyExtended($foreignObject, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $pivot_limit = Array()) {
|
|
|
|
|
+ return new BelongsToManyExtended($this, $foreignObject, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter, $pivot_limit);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (empty($relations)) {
|
|
|
|
|
- return Array();
|
|
|
|
|
- }
|
|
|
|
|
- foreach ($relations as $relation) {
|
|
|
|
|
- $relation->child_element = $obj->findOne(array_merge(Array("id" => Array("=", $relation->$remote_in_pivot)), $remote_filter));
|
|
|
|
|
- $objects[] = $relation;
|
|
|
|
|
- }
|
|
|
|
|
- if (empty($objects)) {
|
|
|
|
|
- return Array();
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * With
|
|
|
|
|
+ *
|
|
|
|
|
+ * Eager load relations on this entity
|
|
|
|
|
+ *
|
|
|
|
|
+ **/
|
|
|
|
|
+ public function with($relations = null, $propagate = null){
|
|
|
|
|
+ if(is_string($relations)){
|
|
|
|
|
+ $relations = [$relations];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $possibleRelations = get_class_methods(static::class);
|
|
|
|
|
+
|
|
|
|
|
+ foreach($relations as $relation){
|
|
|
|
|
+ if(in_array($relation, $possibleRelations)){
|
|
|
|
|
+ $this->$relation = $this->$relation(true)->get();
|
|
|
|
|
+ }
|
|
|
|
|
+ if($this->$relation){
|
|
|
|
|
+ $this->$relation->with($propagate);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- return $objects;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ return $this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
}
|
|
}
|