From 477f3696baaf76b32c63599b45422bfeb56d366f Mon Sep 17 00:00:00 2001 From: ahwelp Date: Tue, 5 Apr 2022 20:03:21 +0000 Subject: [PATCH] =?UTF-8?q?Mudan=C3=A7as?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ORM/BelongsTo.php | 1 - src/ORM/BelongsToMany.php | 16 ++++----- src/ORM/Collection.php | 4 ++- src/ORM/DBInstance.php | 1 + src/ORM/Entity.php | 70 +++++++++++++++++++++++++-------------- 5 files changed, 57 insertions(+), 35 deletions(-) diff --git a/src/ORM/BelongsTo.php b/src/ORM/BelongsTo.php index 2274f3e..203e6b5 100644 --- a/src/ORM/BelongsTo.php +++ b/src/ORM/BelongsTo.php @@ -16,7 +16,6 @@ class BelongsTo { $this->localElement = $localElement; } - function get() { return $this->foreignObject::findOne(Array($this->remoteField => Array('=', $this->localElement->{$this->localField}))); } diff --git a/src/ORM/BelongsToMany.php b/src/ORM/BelongsToMany.php index 20137fd..c41642b 100644 --- a/src/ORM/BelongsToMany.php +++ b/src/ORM/BelongsToMany.php @@ -23,27 +23,27 @@ class BelongsToMany { } function get() { - + $limitsSql = ''; foreach ($this->remoteLimit as $key => $value) { $limitsSql .= "$key $value "; } - $pivotTable = '{' . $this->pivotTable . '}'; + $pivotTable = '{' . $this->pivotTable . '}'; $sql = "SELECT $this->remoteInPivot FROM $pivotTable WHERE $this->localInPivot = ? $limitsSql"; - + $relations = DBInstance::query($sql, Array($this->localObject->id), $this->localObject::_connectionName); - - $ids = Array(); + + $ids = Array(); foreach ($relations as $relation) { $ids[] = $relation->{$this->remoteInPivot}; } - + if (empty($ids)) { 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); } } diff --git a/src/ORM/Collection.php b/src/ORM/Collection.php index 58c6fdc..987a996 100644 --- a/src/ORM/Collection.php +++ b/src/ORM/Collection.php @@ -100,11 +100,13 @@ class Collection implements \Countable, \IteratorAggregate, \ArrayAccess switch (get_class($relation)) { case 'ORM\HasMany': + $this->items[$key]->$connection = $this->items[$key]->$connection()->get(); break; case 'ORM\HasOne': + $this->items[$key]->$connection = $this->items[$key]->$connection()->get(); break; case 'ORM\BelongsTo': - $searchs['belongsTo'][] = []; + $this->items[$key]->$connection = $this->items[$key]->$connection()->get(); break; case 'ORM\BelongsToMany': $this->items[$key]->$connection = $this->items[$key]->$connection()->get(); diff --git a/src/ORM/DBInstance.php b/src/ORM/DBInstance.php index af47879..b8186a7 100755 --- a/src/ORM/DBInstance.php +++ b/src/ORM/DBInstance.php @@ -96,6 +96,7 @@ class DBInstance */ public static function queryOne($sql, $data = null, $instance = 'default') { + $sql = self::addPrefix($sql, $instance); if ($data) { diff --git a/src/ORM/Entity.php b/src/ORM/Entity.php index 8adcccf..a1a837d 100755 --- a/src/ORM/Entity.php +++ b/src/ORM/Entity.php @@ -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 ($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])) { 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; } }