[Solar-svn] Revision 3109

pmjones at solarphp.com pmjones at solarphp.com
Sun Apr 13 08:45:50 CDT 2008


Solar_Sql_Model_Related_HasMany: [CHG] Better grouping of internal logic.


Modified: trunk/Solar/Sql/Model/Related/HasMany.php
===================================================================
--- trunk/Solar/Sql/Model/Related/HasMany.php	2008-04-13 13:44:24 UTC (rev 3108)
+++ trunk/Solar/Sql/Model/Related/HasMany.php	2008-04-13 13:45:48 UTC (rev 3109)
@@ -36,43 +36,41 @@
     public function modSelectEager($select)
     {
         if (empty($this->through)) {
-            // less-complex "has many" relationship.
+            // less-complex "has many" relationship. use the standard support
+            // method.
             $this->_modSelectEager($select);
+        } else {
+            // more-complex "has many through" relationship.
+            // join the native table to the mapping table.
+            $table = "{$this->through_table} AS {$this->through_alias}";
+            $where = "{$this->native_alias}.{$this->native_col} = "
+                   . "{$this->through_alias}.{$this->through_native_col}";
+        
+            $select->leftJoin($table, $where);
             
+            // join the mapping table to the foreign table.
+            $table = "{$this->foreign_table} AS {$this->foreign_alias}";
+            $where = "{$this->through_alias}.{$this->through_foreign_col} = "
+                   . "{$this->foreign_alias}.{$this->foreign_col}";
+        
+            $select->leftJoin($table, $where);
+        
             // make the rows distinct, so we only get one row regardless of
             // the number of related rows (since we're not selecting cols).
             $select->distinct(true);
-            
-            // done!
-            return;
+        
+            // honor foreign inheritance
+            if ($this->foreign_inherit_col) {
+                $select->where(
+                    "{$this->foreign_alias}.{$this->foreign_inherit_col} = ?",
+                    $this->foreign_inherit_val
+                );
+            }
         }
         
-        // more-complex "has many through" relationship.
-        // join the native table to the mapping table.
-        $join_table = "{$this->through_table} AS {$this->through_alias}";
-        $join_where = "{$this->native_alias}.{$this->native_col} = "
-                    . "{$this->through_alias}.{$this->through_native_col}";
-        
-        $select->leftJoin($join_table, $join_where);
-        
-        // join the mapping table to the foreign table.
-        $join_table = "{$this->foreign_table} AS {$this->foreign_alias}";
-        $join_where = "{$this->through_alias}.{$this->through_foreign_col} = "
-                    . "{$this->foreign_alias}.{$this->foreign_col}";
-        
-        $select->leftJoin($join_table, $join_where);
-        
         // make the rows distinct, so we only get one row regardless of
         // the number of related rows (since we're not selecting cols).
         $select->distinct(true);
-        
-        // honor foreign inheritance
-        if ($this->foreign_inherit_col) {
-            $select->where(
-                "{$this->foreign_alias}.{$this->foreign_inherit_col} = ?",
-                $this->foreign_inherit_val
-            );
-        }
     }
     
     /**
@@ -92,32 +90,37 @@
     public function modSelectCountPages($select)
     {
         if (empty($this->through)) {
-            // less-complex "has many" relationship.
-            return parent::modSelectCountPages($select);
-        }
+            // less-complex "has many" relationship. use the standard parent
+            // method.
+            parent::modSelectCountPages($select);
+        } else {
+            // more-complex "has many through" relationship.
+            // join the native table to the mapping table.
+            $table = "{$this->through_table} AS {$this->through_alias}";
+            $where = "{$this->native_alias}.{$this->native_col} = "
+                   . "{$this->through_alias}.{$this->through_native_col}";
         
-        // more-complex "has many through" relationship.
-        // join the native table to the mapping table.
-        $join_table = "{$this->through_table} AS {$this->through_alias}";
-        $join_where = "{$this->native_alias}.{$this->native_col} = "
-                    . "{$this->through_alias}.{$this->through_native_col}";
+            $select->leftJoin($table, $where);
         
-        $select->leftJoin($join_table, $join_where);
+            // join the mapping table to the foreign table.
+            $table = "{$this->foreign_table} AS {$this->foreign_alias}";
+            $where = "{$this->through_alias}.{$this->through_foreign_col} = "
+                   . "{$this->foreign_alias}.{$this->foreign_col}";
         
-        // join the mapping table to the foreign table.
-        $join_table = "{$this->foreign_table} AS {$this->foreign_alias}";
-        $join_where = "{$this->through_alias}.{$this->through_foreign_col} = "
-                    . "{$this->foreign_alias}.{$this->foreign_col}";
+            $select->leftJoin($table, $where);
         
-        $select->leftJoin($join_table, $join_where);
+            // honor foreign inheritance
+            if ($this->foreign_inherit_col) {
+                $select->where(
+                    "{$this->foreign_alias}.{$this->foreign_inherit_col} = ?",
+                    $this->foreign_inherit_val
+                );
+            }
+        }
         
-        // honor foreign inheritance
-        if ($this->foreign_inherit_col) {
-            $select->where(
-                "{$this->foreign_alias}.{$this->foreign_inherit_col} = ?",
-                $this->foreign_inherit_val
-            );
-        }
+        // make the rows distinct, so we only get one row regardless of
+        // the number of related rows (since we're not selecting cols).
+        $select->distinct(true);
     }
     
     /**




More information about the Solar-svn mailing list