Can the the default conditions operator be changed from OR to AND on the Taggable extension?

StackOverflow https://stackoverflow.com//questions/12679389

  •  12-12-2019
  •  | 
  •  

Question

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

I want to retrieve the records that are tagged with any of the following windows, windows7, windowsXp.

By default the tags are generating a condition which are AND-ed. I want to use the OR operator for the tags. So if the record contains windows, windows7 but not windowsXp it won't be retrieved.

I've managed to find a workaround, by editing the getFindByTagsCriteria() in the ETaggableBehavior.php that comes in the extension folder.

/**
     * 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;
    }

I would really appreciate any other way without having to modify the plugin itself.

Was it helpful?

Solution 2

Sam from Yii development team helped me solve this by adding two more functions to the 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();
}

OTHER TIPS

What I'd do here is set the withTags() method in your model to take an array value, for example something like this:

/**
 * @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;
}

This way you should be able to call your named scope like so:

$tags = array('windows', 'windows7', 'windowsXp'),
$tagged = Os::model()->withTags($tags)->findAll();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top