0
$tagged = Os :: model()-> withTags("windows, windows7, windowsXp")-> find();

我想检索标记有以下任何 windowswindows7windowsXp的记录。

默认情况下,这些标签会生成一个 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;
    }

我真的很感激任何其他方式,而不必修改插件本身。

4

2 回答 2

1

我在这里要做的是在你的模型中设置 withTags() 方法来获取一个数组值,例如这样的:

/**
 * @param array $tags List of tags to search for
 * @return named scope
 */
public function withTags($tags)
{
    $condition = '1';
    $params = array();
    foreach($tags as $key=>$value)
    {
        $condition.=' OR tag = :tag'.$key;
        $params[':tag'.$key] = $value;
    }
    $this->getDbCriteria()->mergeWith(array(
        'condition'=>$condition,
        'params'=>$params,
    ));

    return $this;
}

这样,您应该能够像这样调用您的命名范围:

$tags = array('windows', 'windows7', 'windowsXp'),
$tagged = Os::model()->withTags($tags)->findAll();
于 2012-10-02T07:53:25.413 回答
0

Yii 开发团队的 Sam 通过在ETaggableBehavior.php中添加另外两个函数帮助我解决了这个问题

/**
 * 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();
}
于 2013-02-09T14:37:44.763 回答