]> git.andy128k.dev Git - ipf-legacy-orm.git/commitdiff
separate relation dql
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 17 Aug 2013 07:00:57 +0000 (10:00 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 17 Aug 2013 07:00:57 +0000 (10:00 +0300)
ipf/orm/relation.php
ipf/orm/relation/association.php
ipf/orm/relation/foreignkey.php
ipf/orm/relation/localkey.php

index da16b0e04015fbc01e966f7d130bb6e279351fed..04284ec4b24128a923eae9a4e001476d6d054511 100644 (file)
@@ -157,16 +157,7 @@ abstract class IPF_ORM_Relation implements ArrayAccess
                 $this->definition['type'] == IPF_ORM_Relation::ONE_COMPOSITE);
     }
 
-    public function getRelationDql($count)
-    {
-        $component = $this->getTable()->getComponentName();
-
-        $dql  = 'FROM ' . $component
-              . ' WHERE ' . $component . '.' . $this->definition['foreign']
-              . ' IN (' . substr(str_repeat('?, ', $count), 0, -2) . ')';
-
-        return $dql;
-    }
+    abstract public function getRelationDql($count, $context);
 
     abstract public function fetchRelatedFor(IPF_ORM_Record $record);
 
index 93dfffc495ace5211f909fb12d91ceb9cc5f6f70..29fc2b295bb47d216e6147465d130f0ab2b0c2c6 100644 (file)
@@ -11,23 +11,20 @@ class IPF_ORM_Relation_Association extends IPF_ORM_Relation
         return $this->definition['refTable'];
     }
 
-    public function getRelationDql($count, $context = 'record')
+    public function getRelationDql($count, $context)
     {
-        $table = $this->definition['refTable'];
         $component = $this->definition['refTable']->getComponentName();
+        $thisTable = $this->getTable()->getComponentName();
 
+        $sub = substr(str_repeat('?, ', $count), 0, -2);
         switch ($context) {
             case "record":
-                $sub  = substr(str_repeat("?, ", $count),0,-2);
-                $dql  = 'FROM ' . $this->getTable()->getComponentName();
-                $dql .= '.' . $component;
-                $dql .= ' WHERE ' . $this->getTable()->getComponentName()
-                . '.' . $component . '.' . $this->definition['local'] . ' IN (' . $sub . ')';
+                $dql = 'FROM ' . $thisTable . '.' . $component .
+                      ' WHERE ' . $thisTable . '.' . $component . '.' . $this->definition['local'] . ' IN (' . $sub . ')';
                 break;
             case "collection":
-                $sub  = substr(str_repeat("?, ", $count),0,-2);
-                $dql  = 'FROM ' . $component . '.' . $this->getTable()->getComponentName();
-                $dql .= ' WHERE ' . $component . '.' . $this->definition['local'] . ' IN (' . $sub . ')';
+                $dql = 'FROM ' . $component . '.' . $thisTable .
+                      ' WHERE ' . $component . '.' . $this->definition['local'] . ' IN (' . $sub . ')';
                 break;
         }
         return $dql;
@@ -39,9 +36,10 @@ class IPF_ORM_Relation_Association extends IPF_ORM_Relation
         if (empty($id) || ! $this->definition['table']->getAttribute(IPF_ORM::ATTR_LOAD_REFERENCES)) {
             $coll = new IPF_ORM_Collection($this->getTable());
         } else {
-            $coll = $this->getTable()->getConnection()->query($this->getRelationDql(1), array($id));
+            $coll = $this->getTable()->getConnection()->query($this->getRelationDql(1, 'record'), array($id));
         }
         $coll->setReference($record, $this);
         return $coll;
     }
-}
\ No newline at end of file
+}
+
index 8830a4bedf4ba928e3a1378296bb00595de4eada..b0c64f0021e0ec34b5292e17d1dbfd2dcf388d16 100644 (file)
@@ -34,7 +34,7 @@ class IPF_ORM_Relation_ForeignKey extends IPF_ORM_Relation
                 
                 $related = new IPF_ORM_Collection($this->getTable());
             } else {
-                $query      = $this->getRelationDql(1);
+                $query      = $this->getRelationDql(1, '');
                 $related    = $this->getTable()->getConnection()->query($query, $id);
             }
             $related->setReference($record, $this);
@@ -42,6 +42,18 @@ class IPF_ORM_Relation_ForeignKey extends IPF_ORM_Relation
         return $related;
     }
 
+    public function getRelationDql($count, $context)
+    {
+        $table = $this->getTable();
+        $component = $table->getComponentName();
+
+        $dql  = 'FROM ' . $component
+              . ' WHERE ' . $component . '.' . $this->definition['foreign']
+              . ' IN (' . substr(str_repeat('?, ', $count), 0, -2) . ')';
+
+        return $dql;
+    }
+
     public function getCondition($alias = null)
     {
         if ( ! $alias) {
index 635402b22e1921b2378dcbde906bc5b6393ba754..edd225f63cc45f00b272d25a8e98788f357499b1 100644 (file)
@@ -28,6 +28,14 @@ class IPF_ORM_Relation_LocalKey extends IPF_ORM_Relation
         return $related;
     }
 
+    public function getRelationDql($count, $context)
+    {
+        $component = $this->getTable()->getComponentName();
+        return 'FROM ' . $component .
+              ' WHERE ' . $component . '.' . $this->definition['foreign'] .
+              ' IN (' . substr(str_repeat('?, ', $count), 0, -2) . ')';
+    }
+
     public function getCondition($alias = null)
     {
         if ( ! $alias) {