Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 85 additions & 2 deletions ETaggableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,59 @@ protected function getFindByTagsCriteria($tags) {

return $criteria;
}
/**
* Get criteria to limit query by tag key
* @param int $tagId
* @return CDbCriteria
*/
protected function getFindByTagIdCriteria($tagId){
$criteria = new CDbCriteria();

if(!empty($tagId)){
$pk = $this->getOwner()->tableSchema->primaryKey;
$conn = $this->getConnection();
$tagId = $conn->quoteValue($tagId);
$criteria->select = 't.*';
$criteria->join =
"JOIN {$this->getTagBindingTableName()} bt ON t.{$pk} = bt.{$this->getModelTableFkName()} AND bt.{$this->tagBindingTableTagId} = $tagId";
}

if($this->getScopeCriteria()){
$criteria->mergeWith($this->getScopeCriteria());
}
return $criteria;
}

/**
* Get criteria to limit query by tags keys
* @param array $tagsIds
* @return CDbCriteria
*/
protected function getFindByTagsIdsCriteria($tagsIds){
$criteria = new CDbCriteria();

if(!empty($tagsIds)){
$condition = 'IN (';
$conn = $this->getConnection();
foreach($tagsIds as $tagId){
$tagId = $conn->quoteValue($tagId);
$condition .= $tagId.',';
}
$condition = rtrim($condition,',').')';
$pk = $this->getOwner()->tableSchema->primaryKey;
$criteria->select = 't.*';
$criteria->join =
"JOIN {$this->getTagBindingTableName()} bt ON t.{$pk} = bt.{$this->getModelTableFkName()} AND bt.{$this->tagBindingTableTagId} $condition";
$criteria->group = $this->getModelTableFkName();
$criteria->having = 'COUNT(*) = ' . count($tagsIds);
}

if($this->getScopeCriteria()){
$criteria->mergeWith($this->getScopeCriteria());
}
return $criteria;
}

/**
* Get all possible tags for current model class.
* @param CDbCriteria $criteria
Expand Down Expand Up @@ -611,6 +664,26 @@ public function taggedWith($tags) {

return $this->getOwner();
}

/**
* Limit current AR query to have all tags specified by their keys.
* @param int|array $tags
* @return CActiveRecord
*/
public function taggedWithIds($tags) {
$tags = $this->toTagsArray($tags);

if(!empty($tags)){
$criteria = count($tags>1) ?
$this->getFindByTagsIdsCriteria($tags) :
$this->getFindByTagIdCriteria($tags[0]);

$this->getOwner()->getDbCriteria()->mergeWith($criteria);
}

return $this->getOwner();
}

/**
* Alias of {@link taggedWith()}.
* @param string|array $tags
Expand All @@ -619,6 +692,16 @@ public function taggedWith($tags) {
public function withTags($tags) {
return $this->taggedWith($tags);
}

/**
* Alias of {@link taggedWithIds()}.
* @param int|array $tags
* @return CActiveRecord
*/
public function withTagsIds($tags) {
return $this->taggedWithIds($tags);
}

/**
* Delete all present tag bindings.
* @return void
Expand All @@ -630,8 +713,8 @@ protected function deleteTags() {
$conn->createCommand(
sprintf(
"DELETE
FROM `%s`
WHERE %s = %d",
FROM `%s`
WHERE %s = %d",
$this->getTagBindingTableName(),
$this->getModelTableFkName(),
$this->getOwner()->primaryKey
Expand Down