|
|
@@ -15,7 +15,8 @@ abstract class Entity {
|
|
|
'_timestamps',
|
|
|
'_softdelete',
|
|
|
'_tableName',
|
|
|
- '_idPolice'
|
|
|
+ '_idPolice',
|
|
|
+ '_ignore'
|
|
|
);
|
|
|
const _properties = Array();
|
|
|
|
|
|
@@ -53,7 +54,7 @@ abstract class Entity {
|
|
|
if (!isset($this->$key)) {
|
|
|
return 0;
|
|
|
}
|
|
|
- return $this->toArray()[$key];
|
|
|
+ return $this->$key;
|
|
|
}
|
|
|
|
|
|
public function __toString() {
|
|
|
@@ -66,11 +67,23 @@ abstract class Entity {
|
|
|
return static::class;
|
|
|
}
|
|
|
|
|
|
+ /*public function __call($name, $arguments){
|
|
|
+ dd($name);
|
|
|
+ }*/
|
|
|
+
|
|
|
public function charge($payload) {
|
|
|
|
|
|
- $elements = array_diff(static::_properties, self::_ignore, $this->_ignore);
|
|
|
+ $properties = [];
|
|
|
+
|
|
|
+ foreach( get_object_vars($this) as $key => $property){
|
|
|
+ if(substr($key, 0, 1) != '_'){
|
|
|
+ $properties[] = $key;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $elements = array_diff(array_merge($properties, static::_properties), self::_ignore);
|
|
|
|
|
|
- foreach ($elements as $key => $property) {
|
|
|
+ foreach ($elements as $key => $property) {
|
|
|
if ($property == 'id' && !isset($payload[$property])) {
|
|
|
continue;
|
|
|
}
|
|
|
@@ -196,11 +209,11 @@ abstract class Entity {
|
|
|
$this->fill(DBInstance::queryPrepare($sql, Array($id), static::_connectionName)[0]);
|
|
|
}
|
|
|
|
|
|
- public function delete() {
|
|
|
+ public function delete() {
|
|
|
if (isset($this->_softdelete) && $this->_softdelete) {
|
|
|
$this->deleted_at = date('Y-m-d h:m:s');
|
|
|
$this->update();
|
|
|
- }else{
|
|
|
+ }else{
|
|
|
$this->purge();
|
|
|
}
|
|
|
}
|
|
|
@@ -214,7 +227,7 @@ abstract class Entity {
|
|
|
if (isset($this->_softdelete) && $this->_softdelete) {
|
|
|
$this->deleted_at = null;
|
|
|
$this->update();
|
|
|
- }else{
|
|
|
+ }else{
|
|
|
throw new \Exception('Not soft deleteble');
|
|
|
}
|
|
|
}
|
|
|
@@ -225,6 +238,7 @@ abstract class Entity {
|
|
|
foreach ($data as $key => $value) {
|
|
|
$this->$key = $value;
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -256,7 +270,7 @@ abstract class Entity {
|
|
|
} else {
|
|
|
$sql = "SELECT $criteria FROM {" . static::_tableName . "} $limits_sql";
|
|
|
}
|
|
|
- error_log($sql);
|
|
|
+
|
|
|
$results = DBInstance::query($sql, Array(), static::_connectionName);
|
|
|
$objects = new Collection;
|
|
|
|
|
|
@@ -322,7 +336,7 @@ abstract class Entity {
|
|
|
if(empty($criterias)){
|
|
|
$criterias = Array(1 => ['=', 1]);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
$select_sql = "";
|
|
|
$criteria_sql = "";
|
|
|
$limits_sql = "";
|
|
|
@@ -380,11 +394,11 @@ abstract class Entity {
|
|
|
$object->fill($value);
|
|
|
$objects->addItem($object);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if (empty($objects)) {
|
|
|
- return false; //new $this->classname;
|
|
|
+ return false; //new $this->classname;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return $objects[0];
|
|
|
}
|
|
|
|
|
|
@@ -405,8 +419,8 @@ abstract class Entity {
|
|
|
foreach ($limits as $key => $value) {
|
|
|
$limits_sql .= "$key $value ";
|
|
|
}
|
|
|
-
|
|
|
- foreach ($criterias as $key => $criteria) {
|
|
|
+
|
|
|
+ foreach ($criterias as $key => $criteria) {
|
|
|
if ($criteria_sql != "" && substr($criteria[0], 0, 2) == 'OR') {
|
|
|
$criteria_sql .= " ";
|
|
|
} else if ($criteria_sql != "" ) {
|
|
|
@@ -418,10 +432,10 @@ abstract class Entity {
|
|
|
} else {
|
|
|
$criteria_sql .= " ";
|
|
|
}
|
|
|
-
|
|
|
- if (substr($criteria[0], 0, 2) == "OR") {
|
|
|
+
|
|
|
+ if (substr($criteria[0], 0, 2) == "OR") {
|
|
|
$criteria[0] = str_replace('OR', '', $criteria[0]);
|
|
|
- $criteria_sql .= "OR $key::text $criteria[0] ('%$criteria[1]%')";
|
|
|
+ $criteria_sql .= "OR $key $criteria[0] ('%$criteria[1]%')";
|
|
|
} else if (is_string($criteria[1])) {
|
|
|
$criteria_sql .= "$key $criteria[0] '$criteria[1]'";
|
|
|
} else if (is_array($criteria[1])) {
|
|
|
@@ -436,7 +450,7 @@ abstract class Entity {
|
|
|
foreach ($select as $value) {
|
|
|
$select_sql .= $value . ', ';
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
$select_sql = rtrim($select_sql, ', ');
|
|
|
|
|
|
if (static::_softdelete && !$trashed) {
|
|
|
@@ -454,9 +468,11 @@ abstract class Entity {
|
|
|
$object->fill($value);
|
|
|
$objects->addItem($object);
|
|
|
}
|
|
|
+
|
|
|
if (empty($objects)) {
|
|
|
return Array();
|
|
|
}
|
|
|
+
|
|
|
return $objects;
|
|
|
}
|
|
|
|
|
|
@@ -585,26 +601,30 @@ abstract class Entity {
|
|
|
|
|
|
/**
|
|
|
* With
|
|
|
- *
|
|
|
+ *
|
|
|
* Eager load relations on this entity
|
|
|
- *
|
|
|
+ *
|
|
|
**/
|
|
|
public function with($relations = null, $propagate = null){
|
|
|
if(is_string($relations)){
|
|
|
$relations = [$relations];
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ if(!$relations){
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
$possibleRelations = get_class_methods(static::class);
|
|
|
-
|
|
|
+
|
|
|
foreach($relations as $relation){
|
|
|
- if(in_array($relation, $possibleRelations)){
|
|
|
- $this->$relation = $this->$relation(true)->get();
|
|
|
+ if(in_array($relation, $possibleRelations)){
|
|
|
+ $this->$relation = $this->$relation()->get();
|
|
|
}
|
|
|
if($this->$relation){
|
|
|
$this->$relation->with($propagate);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return $this;
|
|
|
}
|
|
|
}
|