Sfoglia il codice sorgente

Adding filter functions to many to many relations, not tested

Artur 7 anni fa
parent
commit
b701df3803
1 ha cambiato i file con 18 aggiunte e 8 eliminazioni
  1. 18 8
      src/BBOrm/Entity.php

+ 18 - 8
src/BBOrm/Entity.php

@@ -198,6 +198,10 @@ abstract class Entity {
         $criteria_sql = "";
         $limits_sql = "";
 
+        foreach ($limits as $key => $value) {
+            $limits_sql .= "$key $value ";
+        }
+
         foreach ($criterias as $key => $criteria) {
             if ($criteria_sql != "") {
                 $criteria_sql .= " AND ";
@@ -251,9 +255,10 @@ abstract class Entity {
         return $obj->find_one(Array('id' => Array('=', $this->$local_field)));
     }
 
-    protected function belongs_to_many($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot) {
-        $obj = new $foreign_object;
-        $sql = "SELECT $remote_in_pivot FROM $pivot_table WHERE $local_in_pivot = $this->id";
+    protected function belongs_to_many($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $remote_limit = Array()) {
+	$obj = new $foreign_object;
+
+        $sql = "SELECT $remote_in_pivot FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql";
         
         $relations = DBInstance::query($sql, $this->connection_name);
         $ids = Array();
@@ -263,12 +268,17 @@ abstract class Entity {
         if (empty($ids)) {
             return Array();
         }
-        return $obj->find_many(Array('id' => Array('IN', $ids)));
+        return $obj->find_many( array_merge( Array( 'id' => Array('IN', $ids) ), $remote_filter), Array('*'), $remote_limit );
     }
 
-    protected function belongs_to_many_extended($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot) {
-        $obj = new $foreign_object;
-        $sql = "SELECT * FROM $pivot_table WHERE $local_in_pivot = $this->id";
+    protected function belongs_to_many_extended($foreign_object, $pivot_table, $local_in_pivot, $remote_in_pivot, $remote_filter = Array(), $pivot_limits = Array()) {
+	    $obj = new $foreign_object;
+        $limits_sql = '';
+        foreach ($pivot_limits as $key => $value) {
+            $limits_sql .= "$key $value ";
+        }
+	    
+        $sql = "SELECT * FROM $pivot_table WHERE $local_in_pivot = $this->id $limits_sql";
         $relations = DBInstance::query($sql, $this->connection_name);
         $objects = Array();
         
@@ -276,7 +286,7 @@ abstract class Entity {
             return Array();
         }        
         foreach ($relations as $relation) {
-            $relation->child_element = $obj->find_one(Array("id" => Array("=", $relation->$remote_in_pivot)));
+            $relation->child_element = $obj->find_one(array_merge(Array("id" => Array("=", $relation->$remote_in_pivot), $remote_filter) ));
             $objects[] = $relation;
         }
         if (empty($objects)) {