[Solar-svn] Revision 3113

pmjones at solarphp.com pmjones at solarphp.com
Sun Apr 13 09:30:39 CDT 2008


Solar_Model_Tags
----------------

* [FIX] The has-many-nodes-through-taggings relation now uses the correct
  'through_key' value.

* [DEL] Method _newSelectWithCount() is now entirely unnecessary, because the
  new Solar_Sql_Model::newSelect() method now adds all types of eager joins
  for us via $params.

* [REF] Refactored method fetchAllWithCount() to use only fetchAll($params),
  instead of building a custom SELECT and running it through the previous
  model _fetchAll() support method.

* [REF] Refactored method fetchAllByOwnerHandle() to use only
  fetchAllWithCount($params), instead of building a custom SELECT and running
  it through the previous model _fetchAll() support method.



Modified: trunk/Solar/Model/Tags.php
===================================================================
--- trunk/Solar/Model/Tags.php	2008-04-13 14:18:05 UTC (rev 3112)
+++ trunk/Solar/Model/Tags.php	2008-04-13 14:30:39 UTC (rev 3113)
@@ -68,6 +68,7 @@
         $this->_hasMany('nodes', array(
             'foreign_class' => 'nodes',
             'through'       => 'taggings',
+            'through_key'   => 'node_id',
         ));
     }
     
@@ -83,120 +84,39 @@
      */
     public function fetchAllWithCount($params = null)
     {
-        $params = $this->fixSelectParams($params);
-        $select = $this->_newSelectWithCount($params);
-        return $this->_fetchAll($select, $params);
-    }
-    
-    /**
-     * 
-     * Fetches a collection of all tags applied by a particular owner, as
-     * identified by that user's "handle".
-     * 
-     * @param string $owner_handle Only select tags in use by this handle.
-     * 
-     * @param array $params Added parameters for the select.
-     * 
-     * @return Solar_Model_Tags_Collection
-     * 
-     */
-    public function fetchAllByOwnerHandle($owner_handle, $params = null)
-    {
-        $owner_handle = trim($owner_handle);
-        if (! $owner_handle) {
-            return $this->fetchAll($params);
-        }
-        
-        // setup
-        $params = $this->fixSelectParams($params);
-        $select = $this->newSelect($params['eager']);
-        
-        // catalog entries for joining
-        $taggings = $this->getRelated('taggings');
-        $nodes    = $this->getRelated('nodes');
-        
         // primary key on this table alias; e.g., tags.id
         $native_primary = "{$this->_model_name}.{$this->_primary_col}";
         
-        // add a tag-count column
+        // fetch all columns, plus a count column
+        $params['cols'] = $this->_fetch_cols;
         $params['cols'][] = "COUNT($native_primary) AS count";
         
-        // build the select
-        $select->distinct($params['distinct'])
-               ->from("{$this->_table_name} AS {$this->_model_name}", $params['cols'])
-               // join taggings on tags
-               ->join(
-                   "{$taggings->foreign_table} AS {$taggings->foreign_alias}",
-                   "{$taggings->foreign_alias}.tag_id = $native_primary"
-               )
-               // join nodes on taggings
-               ->join(
-                   "{$nodes->foreign_table} AS {$nodes->foreign_alias}",
-                   "{$nodes->foreign_alias}.id = {$taggings->foreign_alias}.node_id"
-               )
-               // select for the owner_handle
-               ->where("{$nodes->foreign_alias}.owner_handle = ?", $owner_handle)
-               // group on primary key for counts
-               ->group($native_primary)
-               // user-provided ORDER, paging, etc
-               ->multiWhere($params['where'])
-               ->order($params['order'])
-               ->setPaging($params['paging'])
-               ->limitPage($params['page'])
-               ->bind($params['bind']);
+        // group on primary key for counts
+        $params['group'][] = $native_primary;
         
-        // fetch
-        $select = $this->_newSelectWithCount($params);
-        $select->where("{$nodes->foreign_alias}.owner_handle = ?", $owner_handle);
-        return $this->_fetchAll($select, $params);
+        // eager-join to nodes for the count of nodes
+        $params['eager'][] = 'nodes';
+        
+        // done with params
+        return $this->fetchAll($params);
     }
     
     /**
      * 
-     * Support method to add the tag-count to a selection tool.
+     * Fetches a collection of all tags applied by a particular owner (as
+     * identified by that user's "handle") with the count of nodes using each
+     * tag.
      * 
+     * @param string $owner_handle Only select tags in use by this handle.
+     * 
      * @param array $params Added parameters for the select.
      * 
-     * @return Solar_Sql_Select
+     * @return Solar_Model_Tags_Collection
      * 
      */
-    protected function _newSelectWithCount($params)
+    public function fetchAllByOwnerHandle($owner_handle, $params = null)
     {
-        // params should have been fixed by this point
-        $select = $this->newSelect($params['eager']);
-        
-        // catalog entries for joining
-        $taggings = $this->getRelated('taggings');
-        $nodes    = $this->getRelated('nodes');
-        
-        // primary key on this table alias; e.g., tags.id
-        $native_primary = "{$this->_model_name}.{$this->_primary_col}";
-        
-        // add a tag-count column
-        $params['cols'][] = "COUNT($native_primary) AS count";
-        
-        // build the select
-        $select->distinct($params['distinct'])
-               ->from("{$this->_table_name} AS {$this->_model_name}", $params['cols'])
-               // join taggings on tags
-               ->join(
-                   "{$taggings->foreign_table} AS {$taggings->foreign_alias}",
-                   "{$taggings->foreign_alias}.tag_id = $native_primary"
-               )
-               // join nodes on taggings
-               ->join(
-                   "{$nodes->foreign_table} AS {$nodes->foreign_alias}",
-                   "{$nodes->foreign_alias}.id = {$taggings->foreign_alias}.node_id"
-               )
-               // group on primary key for counts
-               ->group($native_primary)
-               // user-provided ORDER, paging, etc
-               ->multiWhere($params['where'])
-               ->order($params['order'])
-               ->setPaging($params['paging'])
-               ->limitPage($params['page'])
-               ->bind($params['bind']);
-        
-        return $select;
+        $params['where']['nodes.owner_handle = ?'] = $owner_handle;
+        return $this->fetchAllWithCount($params);
     }
 }




More information about the Solar-svn mailing list