Simple Tagging Component

By Ben Milleare (bgmill)
This is a quick and easy tagging component that allows you to handle text field, comma-separated input.
It first parses the submitted data and then checks for the existence of each tag, before deciding whether to create it or not. It expects a model called Tag in a HABTM association with the calling controller.

Save this as /app/controllers/components/tagging.php and include it in your $components array.

Component Class:

Download code <?php 
class TaggingComponent extends Object
{
function 
tagParse($tagdump) {
    
// first we need to split the tags up and strip whitespace at the beginning and end
    // not ALL whitespace as we're allowing multiple word tags
    
$tempTagArray preg_split('/,/',$tagdump);
    
$tagArray = array();
    foreach (
$tempTagArray as $t) {
        
$t trim($t);
        if (
strlen($t)>0) {
            
$tagArray[] = $t;
        }
    }
    
$Tag = new Tag// create a new Tag object
    
$tagInfo = array(); // create a new array to store tag id and name combo's from db
    
foreach ($tagArray as $t) {
        if (
$res $Tag->findByName($t)) {
            
// tag exists already, add it to our array
            
$tagInfo[] = $res['Tag']['id'];
        } else {
            
// tag doesn't exist, lets add it
            
$Tag->save(array('id'=>'','name'=>$t));
            
// now we can add this to our array
            
$tagInfo[] = sprintf($Tag->getLastInsertID());
        }
        unset(
$res);
    }
    return 
$tagInfo;
}

}
?>

Usage:

Controller Class:

Download code <?php 
...
$this->params[’data’][’Tag’][’Tag’] = $this->Tagging->tagParse($theTagField);
...
?>

This will parse the data back into the correct form for simply doing a...

Controller Class:

Download code <?php 
...
$this->controller->save($this->params[’data’]);
...
?>

(originally posted at http://ben.milleare.com/2006/08/29/tagging-in-cakephp/)

 

Comments 70

CakePHP Team Comments Author Comments
 

Comment

1 Trimming Whitespaces

Better solution for trimming whitespaces:

PHP has a function called *trim* which should used for this. It's much faster than this regex-replace.

http://php.net/manual/en/function.trim.php
Posted Oct 8, 2006 by rikman
 

Comment

2 Thanks

Thanks, I was unaware that trim() existed -- you learn something new every day!
Posted Oct 9, 2006 by Ben Milleare
 

Question

3 Why not a Model

This should really be done in the model. Look how we implemented tagging in the bakery code. It is not the best practice to use a model inside of a component. Consider revising to keep data where it belongs.
Posted Oct 17, 2006 by gwoo
 

Bug

4 Model access

I just tested it in one (weird) installation environment, but I get a bug at the following line:

$Tag = new Tag; // create a new Tag object

Solved that changing it to:

$Tag =& new Tag; // create a new Tag object

This is also the component best practice to access the model like said in the manual.
Posted Oct 18, 2006 by Fernando Kreigne
 

Question

5 Help

I really like the idea here. Unfortunately, I'm just starting out with Cake and this walkthrough seems like it's geared more towards experienced bakers. Can you help me understand what exactly I need to get a functional version of this working?

The tagging example in the Cake Manual seemed incomplete... I'm guessing this is based off of that somehow.

Thanks-m
Posted Nov 5, 2006 by matt parrett
 

Comment

6 Tag Schema

I found this article useful

http://forge.mysql.com/wiki/TagSchema
Posted Jul 4, 2008 by sapn