Simple Tagging Component

By Ben Milleare aka "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 Sun, Oct 8th 2006, 16:26 by rikman

Comment

2 Thanks

Thanks, I was unaware that trim() existed -- you learn something new every day!
posted Mon, Oct 9th 2006, 11:41 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 Tue, Oct 17th 2006, 10:50 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 Wed, Oct 18th 2006, 23:45 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 Sun, Nov 5th 2006, 18:17 by matt parrett

Login to Submit a Comment