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})));
} }
+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) {
+26 -6
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,9 +67,21 @@ 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( 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])) { if ($property == 'id' && !isset($payload[$property])) {
@@ -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;
@@ -421,7 +435,7 @@ abstract class Entity {
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])) {
@@ -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;
} }
@@ -594,11 +610,15 @@ abstract class Entity {
$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);