-
-
Save svilenkov/3826994 to your computer and use it in GitHub Desktop.
Revisions
-
svilenkov revised this gist
Oct 3, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,6 +18,7 @@ protected function getFindByAnyTagsCriteria($tags) { $tags = implode(', ', $tags); $criteria->select = 't.*'; $criteria -> distinct = true; $criteria->join .= "JOIN {$this->getTagBindingTableName()} bt ON t.{$pk} = bt.{$this->getModelTableFkName()} JOIN {$this->tagTable} tag ON tag.{$this->tagTablePk} = bt.{$this->tagBindingTableTagId} AND tag.`{$this->tagTableName}` IN ($tags)"; -
samdark created this gist
Oct 3, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ /** * Get criteria to limit query to match any of tags specified * @access private * @param array $tags * @return CDbCriteria */ protected function getFindByAnyTagsCriteria($tags) { $criteria = new CDbCriteria(); $pk = $this->getOwner()->tableSchema->primaryKey; if(!empty($tags)){ $conn = $this->getConnection(); foreach($tags as &$tag) { $tag = $conn->quoteValue($tag); } unset($tag); $tags = implode(', ', $tags); $criteria->select = 't.*'; $criteria->join .= "JOIN {$this->getTagBindingTableName()} bt ON t.{$pk} = bt.{$this->getModelTableFkName()} JOIN {$this->tagTable} tag ON tag.{$this->tagTablePk} = bt.{$this->tagBindingTableTagId} AND tag.`{$this->tagTableName}` IN ($tags)"; } } if($this->getScopeCriteria()){ $criteria->mergeWith($this->getScopeCriteria()); } return $criteria; } /** * Limit current AR query to have any of tags specified. * @param string|array $tags * @return CActiveRecord */ public function taggedWithAnyOf($tags) { $tags = $this->toTagsArray($tags); if(!empty($tags)){ $criteria = $this->getFindByAnyTagsCriteria($tags); $this->getOwner()->getDbCriteria()->mergeWith($criteria); } return $this->getOwner(); }