Mudanças

This commit is contained in:
ahwelp
2022-04-05 20:03:21 +00:00
parent 1669a4279d
commit 477f3696ba
5 changed files with 57 additions and 35 deletions
-1
View File
@@ -16,7 +16,6 @@ class BelongsTo {
$this->localElement = $localElement; $this->localElement = $localElement;
} }
function get() { function get() {
return $this->foreignObject::findOne(Array($this->remoteField => Array('=', $this->localElement->{$this->localField}))); return $this->foreignObject::findOne(Array($this->remoteField => Array('=', $this->localElement->{$this->localField})));
} }
+8 -8
View File
@@ -23,27 +23,27 @@ class BelongsToMany {
} }
function get() { function get() {
$limitsSql = ''; $limitsSql = '';
foreach ($this->remoteLimit as $key => $value) { foreach ($this->remoteLimit as $key => $value) {
$limitsSql .= "$key $value "; $limitsSql .= "$key $value ";
} }
$pivotTable = '{' . $this->pivotTable . '}'; $pivotTable = '{' . $this->pivotTable . '}';
$sql = "SELECT $this->remoteInPivot FROM $pivotTable WHERE $this->localInPivot = ? $limitsSql"; $sql = "SELECT $this->remoteInPivot FROM $pivotTable WHERE $this->localInPivot = ? $limitsSql";
$relations = DBInstance::query($sql, Array($this->localObject->id), $this->localObject::_connectionName); $relations = DBInstance::query($sql, Array($this->localObject->id), $this->localObject::_connectionName);
$ids = Array(); $ids = Array();
foreach ($relations as $relation) { foreach ($relations as $relation) {
$ids[] = $relation->{$this->remoteInPivot}; $ids[] = $relation->{$this->remoteInPivot};
} }
if (empty($ids)) { if (empty($ids)) {
return Array(); return Array();
} }
return $this->foreignObject::findMany(array_merge(Array('id' => Array('IN', $ids)), $this->remoteFilter), Array('*'), $this->remoteLimit); return $this->foreignObject::findMany(array_merge(Array('id' => Array('IN', $ids)), $this->remoteFilter), Array('*'), $this->remoteLimit);
} }
} }
+3 -1
View File
@@ -100,11 +100,13 @@ class Collection implements \Countable, \IteratorAggregate, \ArrayAccess
switch (get_class($relation)) { switch (get_class($relation)) {
case 'ORM\HasMany': case 'ORM\HasMany':
$this->items[$key]->$connection = $this->items[$key]->$connection()->get();
break; break;
case 'ORM\HasOne': case 'ORM\HasOne':
$this->items[$key]->$connection = $this->items[$key]->$connection()->get();
break; break;
case 'ORM\BelongsTo': case 'ORM\BelongsTo':
$searchs['belongsTo'][] = []; $this->items[$key]->$connection = $this->items[$key]->$connection()->get();
break; break;
case 'ORM\BelongsToMany': case 'ORM\BelongsToMany':
$this->items[$key]->$connection = $this->items[$key]->$connection()->get(); $this->items[$key]->$connection = $this->items[$key]->$connection()->get();
+1
View File
@@ -96,6 +96,7 @@ class DBInstance
*/ */
public static function queryOne($sql, $data = null, $instance = 'default') public static function queryOne($sql, $data = null, $instance = 'default')
{ {
$sql = self::addPrefix($sql, $instance); $sql = self::addPrefix($sql, $instance);
if ($data) { if ($data) {
+45 -25
View File
@@ -15,7 +15,8 @@ abstract class Entity {
'_timestamps', '_timestamps',
'_softdelete', '_softdelete',
'_tableName', '_tableName',
'_idPolice' '_idPolice',
'_ignore'
); );
const _properties = Array(); const _properties = Array();
@@ -53,7 +54,7 @@ abstract class Entity {
if (!isset($this->$key)) { if (!isset($this->$key)) {
return 0; return 0;
} }
return $this->toArray()[$key]; return $this->$key;
} }
public function __toString() { public function __toString() {
@@ -66,11 +67,23 @@ abstract class Entity {
return static::class; return static::class;
} }
/*public function __call($name, $arguments){
dd($name);
}*/
public function charge($payload) { public function charge($payload) {
$elements = array_diff(static::_properties, self::_ignore, $this->_ignore); $properties = [];
foreach ($elements as $key => $property) { 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) {
if ($property == 'id' && !isset($payload[$property])) { if ($property == 'id' && !isset($payload[$property])) {
continue; continue;
} }
@@ -196,11 +209,11 @@ 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() { public function delete() {
if (isset($this->_softdelete) && $this->_softdelete) { if (isset($this->_softdelete) && $this->_softdelete) {
$this->deleted_at = date('Y-m-d h:m:s'); $this->deleted_at = date('Y-m-d h:m:s');
$this->update(); $this->update();
}else{ }else{
$this->purge(); $this->purge();
} }
} }
@@ -214,7 +227,7 @@ abstract class Entity {
if (isset($this->_softdelete) && $this->_softdelete) { if (isset($this->_softdelete) && $this->_softdelete) {
$this->deleted_at = null; $this->deleted_at = null;
$this->update(); $this->update();
}else{ }else{
throw new \Exception('Not soft deleteble'); throw new \Exception('Not soft deleteble');
} }
} }
@@ -225,6 +238,7 @@ abstract class Entity {
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
$this->$key = $value; $this->$key = $value;
} }
} }
/** /**
@@ -256,7 +270,7 @@ 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 = new Collection; $objects = new Collection;
@@ -322,7 +336,7 @@ abstract class Entity {
if(empty($criterias)){ if(empty($criterias)){
$criterias = Array(1 => ['=', 1]); $criterias = Array(1 => ['=', 1]);
} }
$select_sql = ""; $select_sql = "";
$criteria_sql = ""; $criteria_sql = "";
$limits_sql = ""; $limits_sql = "";
@@ -380,11 +394,11 @@ abstract class Entity {
$object->fill($value); $object->fill($value);
$objects->addItem($object); $objects->addItem($object);
} }
if (empty($objects)) { if (empty($objects)) {
return false; //new $this->classname; return false; //new $this->classname;
} }
return $objects[0]; return $objects[0];
} }
@@ -405,8 +419,8 @@ 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) { foreach ($criterias as $key => $criteria) {
if ($criteria_sql != "" && substr($criteria[0], 0, 2) == 'OR') { if ($criteria_sql != "" && substr($criteria[0], 0, 2) == 'OR') {
$criteria_sql .= " "; $criteria_sql .= " ";
} else if ($criteria_sql != "" ) { } else if ($criteria_sql != "" ) {
@@ -418,10 +432,10 @@ abstract class Entity {
} else { } else {
$criteria_sql .= " "; $criteria_sql .= " ";
} }
if (substr($criteria[0], 0, 2) == "OR") { if (substr($criteria[0], 0, 2) == "OR") {
$criteria[0] = str_replace('OR', '', $criteria[0]); $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])) { } 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])) {
@@ -436,7 +450,7 @@ abstract class Entity {
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) {
@@ -454,9 +468,11 @@ abstract class Entity {
$object->fill($value); $object->fill($value);
$objects->addItem($object); $objects->addItem($object);
} }
if (empty($objects)) { if (empty($objects)) {
return Array(); return Array();
} }
return $objects; return $objects;
} }
@@ -585,26 +601,30 @@ abstract class Entity {
/** /**
* With * With
* *
* Eager load relations on this entity * Eager load relations on this entity
* *
**/ **/
public function with($relations = null, $propagate = null){ public function with($relations = null, $propagate = null){
if(is_string($relations)){ if(is_string($relations)){
$relations = [$relations]; $relations = [$relations];
} }
if(!$relations){
return $this;
}
$possibleRelations = get_class_methods(static::class); $possibleRelations = get_class_methods(static::class);
foreach($relations as $relation){ foreach($relations as $relation){
if(in_array($relation, $possibleRelations)){ if(in_array($relation, $possibleRelations)){
$this->$relation = $this->$relation(true)->get(); $this->$relation = $this->$relation()->get();
} }
if($this->$relation){ if($this->$relation){
$this->$relation->with($propagate); $this->$relation->with($propagate);
} }
} }
return $this; return $this;
} }
} }