ahwelp 3 سال پیش
والد
کامیت
477f3696ba
5فایلهای تغییر یافته به همراه57 افزوده شده و 35 حذف شده
  1. 0 1
      src/ORM/BelongsTo.php
  2. 8 8
      src/ORM/BelongsToMany.php
  3. 3 1
      src/ORM/Collection.php
  4. 1 0
      src/ORM/DBInstance.php
  5. 45 25
      src/ORM/Entity.php

+ 0 - 1
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})));
     }

+ 8 - 8
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);
     }
 
 }

+ 3 - 1
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();

+ 1 - 0
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) {

+ 45 - 25
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( 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;
     }
 }