| 86 | | $params = $this->fixSelectParams($params); |
|---|
| 87 | | $select = $this->_newSelectWithCount($params); |
|---|
| 88 | | return $this->_fetchAll($select, $params); |
|---|
| | 87 | // primary key on this table alias; e.g., tags.id |
|---|
| | 88 | $native_primary = "{$this->_model_name}.{$this->_primary_col}"; |
|---|
| | 89 | |
|---|
| | 90 | // fetch all columns, plus a count column |
|---|
| | 91 | $params['cols'] = $this->_fetch_cols; |
|---|
| | 92 | $params['cols'][] = "COUNT($native_primary) AS count"; |
|---|
| | 93 | |
|---|
| | 94 | // group on primary key for counts |
|---|
| | 95 | $params['group'][] = $native_primary; |
|---|
| | 96 | |
|---|
| | 97 | // eager-join to nodes for the count of nodes |
|---|
| | 98 | $params['eager'][] = 'nodes'; |
|---|
| | 99 | |
|---|
| | 100 | // done with params |
|---|
| | 101 | return $this->fetchAll($params); |
|---|
| 105 | | $owner_handle = trim($owner_handle); |
|---|
| 106 | | if (! $owner_handle) { |
|---|
| 107 | | return $this->fetchAll($params); |
|---|
| 108 | | } |
|---|
| 109 | | |
|---|
| 110 | | // setup |
|---|
| 111 | | $params = $this->fixSelectParams($params); |
|---|
| 112 | | $select = $this->newSelect($params['eager']); |
|---|
| 113 | | |
|---|
| 114 | | // catalog entries for joining |
|---|
| 115 | | $taggings = $this->getRelated('taggings'); |
|---|
| 116 | | $nodes = $this->getRelated('nodes'); |
|---|
| 117 | | |
|---|
| 118 | | // primary key on this table alias; e.g., tags.id |
|---|
| 119 | | $native_primary = "{$this->_model_name}.{$this->_primary_col}"; |
|---|
| 120 | | |
|---|
| 121 | | // add a tag-count column |
|---|
| 122 | | $params['cols'][] = "COUNT($native_primary) AS count"; |
|---|
| 123 | | |
|---|
| 124 | | // build the select |
|---|
| 125 | | $select->distinct($params['distinct']) |
|---|
| 126 | | ->from("{$this->_table_name} AS {$this->_model_name}", $params['cols']) |
|---|
| 127 | | // join taggings on tags |
|---|
| 128 | | ->join( |
|---|
| 129 | | "{$taggings->foreign_table} AS {$taggings->foreign_alias}", |
|---|
| 130 | | "{$taggings->foreign_alias}.tag_id = $native_primary" |
|---|
| 131 | | ) |
|---|
| 132 | | // join nodes on taggings |
|---|
| 133 | | ->join( |
|---|
| 134 | | "{$nodes->foreign_table} AS {$nodes->foreign_alias}", |
|---|
| 135 | | "{$nodes->foreign_alias}.id = {$taggings->foreign_alias}.node_id" |
|---|
| 136 | | ) |
|---|
| 137 | | // select for the owner_handle |
|---|
| 138 | | ->where("{$nodes->foreign_alias}.owner_handle = ?", $owner_handle) |
|---|
| 139 | | // group on primary key for counts |
|---|
| 140 | | ->group($native_primary) |
|---|
| 141 | | // user-provided ORDER, paging, etc |
|---|
| 142 | | ->multiWhere($params['where']) |
|---|
| 143 | | ->order($params['order']) |
|---|
| 144 | | ->setPaging($params['paging']) |
|---|
| 145 | | ->limitPage($params['page']) |
|---|
| 146 | | ->bind($params['bind']); |
|---|
| 147 | | |
|---|
| 148 | | // fetch |
|---|
| 149 | | $select = $this->_newSelectWithCount($params); |
|---|
| 150 | | $select->where("{$nodes->foreign_alias}.owner_handle = ?", $owner_handle); |
|---|
| 151 | | return $this->_fetchAll($select, $params); |
|---|
| 152 | | } |
|---|
| 153 | | |
|---|
| 154 | | /** |
|---|
| 155 | | * |
|---|
| 156 | | * Support method to add the tag-count to a selection tool. |
|---|
| 157 | | * |
|---|
| 158 | | * @param array $params Added parameters for the select. |
|---|
| 159 | | * |
|---|
| 160 | | * @return Solar_Sql_Select |
|---|
| 161 | | * |
|---|
| 162 | | */ |
|---|
| 163 | | protected function _newSelectWithCount($params) |
|---|
| 164 | | { |
|---|
| 165 | | // params should have been fixed by this point |
|---|
| 166 | | $select = $this->newSelect($params['eager']); |
|---|
| 167 | | |
|---|
| 168 | | // catalog entries for joining |
|---|
| 169 | | $taggings = $this->getRelated('taggings'); |
|---|
| 170 | | $nodes = $this->getRelated('nodes'); |
|---|
| 171 | | |
|---|
| 172 | | // primary key on this table alias; e.g., tags.id |
|---|
| 173 | | $native_primary = "{$this->_model_name}.{$this->_primary_col}"; |
|---|
| 174 | | |
|---|
| 175 | | // add a tag-count column |
|---|
| 176 | | $params['cols'][] = "COUNT($native_primary) AS count"; |
|---|
| 177 | | |
|---|
| 178 | | // build the select |
|---|
| 179 | | $select->distinct($params['distinct']) |
|---|
| 180 | | ->from("{$this->_table_name} AS {$this->_model_name}", $params['cols']) |
|---|
| 181 | | // join taggings on tags |
|---|
| 182 | | ->join( |
|---|
| 183 | | "{$taggings->foreign_table} AS {$taggings->foreign_alias}", |
|---|
| 184 | | "{$taggings->foreign_alias}.tag_id = $native_primary" |
|---|
| 185 | | ) |
|---|
| 186 | | // join nodes on taggings |
|---|
| 187 | | ->join( |
|---|
| 188 | | "{$nodes->foreign_table} AS {$nodes->foreign_alias}", |
|---|
| 189 | | "{$nodes->foreign_alias}.id = {$taggings->foreign_alias}.node_id" |
|---|
| 190 | | ) |
|---|
| 191 | | // group on primary key for counts |
|---|
| 192 | | ->group($native_primary) |
|---|
| 193 | | // user-provided ORDER, paging, etc |
|---|
| 194 | | ->multiWhere($params['where']) |
|---|
| 195 | | ->order($params['order']) |
|---|
| 196 | | ->setPaging($params['paging']) |
|---|
| 197 | | ->limitPage($params['page']) |
|---|
| 198 | | ->bind($params['bind']); |
|---|
| 199 | | |
|---|
| 200 | | return $select; |
|---|
| | 119 | $params['where']['nodes.owner_handle = ?'] = $owner_handle; |
|---|
| | 120 | return $this->fetchAllWithCount($params); |
|---|