可以在Taggable扩展上将默认条件运算符从OR更改为AND吗?

时间:2012-10-01 19:03:58

标签: yii yii-extensions

$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;
    }

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

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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();
}