[Solar-svn] Revision 2770

pmjones at solarphp.com pmjones at solarphp.com
Sun Sep 23 15:10:06 CDT 2007


branch: Solar_Sql_Model

* [ADD] Method fetchAll() now calls new _fetchAll() method to retrieve data and add eager-related records.  Added this so that other fetchAll*() methods in extended classes can get eager loading with less trouble.

* [ADD] Method getRelatedModel() to get a related model class by the relation name (not the class name).

* [CHG] Select-building methods now use new catalog keys 'native_table' and 'native_alias' instead of $this->_table_name and $this->_model_name, for consistency's sake.


Modified: branches/orm/Solar/Sql/Model.php
===================================================================
--- branches/orm/Solar/Sql/Model.php	2007-09-23 20:02:54 UTC (rev 2769)
+++ branches/orm/Solar/Sql/Model.php	2007-09-23 20:10:05 UTC (rev 2770)
@@ -708,7 +708,12 @@
                ->limitPage($params['page'])
                ->bind($params['bind']);
         
-        // fetch
+        // fetch all with eager loading
+        return $this->_fetchAll($select, $params);
+    }
+    
+    protected function _fetchAll($select, $params)
+    {
         $data = $select->fetchAll();
         if ($data) {
             $coll = $this->newCollection($data);
@@ -1339,7 +1344,7 @@
             // build the select
             $spec = $this->newSelect();
             $spec->distinct($params['distinct'])
-                 ->from("{$this->_table_name} AS $this->_model_name", $opts['native_col'])
+                 ->from("{$opts['native_table']} AS {$opts['native_alias']}", $opts['native_col'])
                  ->multiWhere($params['where'])
                  ->group($params['group'])
                  ->having($params['having'])
@@ -1379,9 +1384,9 @@
                 
                 // add the native table ID at the top through a join
                 $select->innerJoin(
-                    "($inner) AS {$this->_model_name}",
-                    "{$opts['through_alias']}.{$opts['through_native_col']} = {$this->_model_name}.{$opts['native_col']}",
-                    "{$opts['native_col']} AS {$this->_model_name}__{$opts['native_col']}"
+                    "($inner) AS {$opts['native_alias']}",
+                    "{$opts['through_alias']}.{$opts['through_native_col']} = {$opts['native_alias']}.{$opts['native_col']}",
+                    "{$opts['native_col']} AS {$opts['native_alias']}__{$opts['native_col']}"
                 );
             }
             
@@ -1414,9 +1419,9 @@
                 $inner = str_replace("\n", "\n\t\t", $spec->fetchSql());
                 // add the native table ID at the top through a join
                 $select->innerJoin(
-                    "($inner) AS {$this->_model_name}",
-                    "{$opts['foreign_alias']}.{$opts['foreign_col']} = {$this->_model_name}.{$opts['native_col']}",
-                    "{$opts['native_col']} AS {$this->_model_name}__{$opts['native_col']}"
+                    "($inner) AS {$opts['native_alias']}",
+                    "{$opts['foreign_alias']}.{$opts['foreign_col']} = {$opts['native_alias']}.{$opts['native_col']}",
+                    "{$opts['native_col']} AS {$opts['native_alias']}__{$opts['native_col']}"
                 );
             }
             
@@ -1447,6 +1452,15 @@
         return $select;
     }
     
+    public function getRelatedModel($name)
+    {
+        $class = $this->_related[$name]['class'];
+        $model = Solar::factory($class, array(
+            'sql' => $this->_sql,
+        ));
+        return $model;
+    }
+    
     /**
      * 
      * Given a record, fetches a related record or collection for a named




More information about the Solar-svn mailing list