$tagged = Os :: model()-> withTags("windows, windows7, windowsXp")-> find();
我想检索标记有以下任何 windows、windows7、windowsXp的记录。
默认情况下,这些标签会生成一个 AND-ed 条件。我想对标签使用 OR 运算符。因此,如果记录包含 windows、windows7 但不包含 windowsXp,它将不会被检索。
通过编辑扩展文件夹中的ETaggableBehavior.php中的getFindByTagsCriteria() ,我设法找到了解决方法。
/**
* Get criteria to limit query by tags.
* @access private
* @param array $tags
* @return CDbCriteria
*/
protected function getFindByTagsCriteria($tags) {
$criteria = new CDbCriteria();
$pk = $this->getOwner()->tableSchema->primaryKey;
if(!empty($tags)){
$conn = $this->getConnection();
$criteria->select = 't.*';
if(count($tags) >0){
$criteria -> join .= "
JOIN {$this->getTagBindingTableName()} bt
ON t.{$pk} = bt.{$this->getModelTableFkName()}
JOIN {$this->tagTable} tag0
ON tag0.{$this->tagTablePk} = bt.{$this->tagBindingTableTagId} AND (";
for($i = 0, $count = count($tags); $i < $count; $i++){
$tag = $conn->quoteValue($tags[$i]);
$criteria->join .= " tag0.`{$this->tagTableName}` = $tag OR";
}
$criteria -> join = rtrim($criteria -> join, "OR");
$criteria -> join .= ")";
}
}
if($this->getScopeCriteria()){
$criteria->mergeWith($this->getScopeCriteria());
}
return $criteria;
}
我真的很感激任何其他方式,而不必修改插件本身。