sfPropelActAsTaggableBehaviorPlugin - 0.5.0

Propel taggable behavior

You are currently browsing
the website for symfony 1

Visit the Symfony2 website

« Back to the Plugins Home


Forgot your password?
Create an account



advanced search
Information Readme Releases Changelog Contribute
Show source | Show as Markdown



This behavior permits to attach tags to Propel objects. It includes tag-clouds generation and helpers to display these clouds.



  • add/remove tag(s) on an object
  • multi-tags object search
  • multi-models selection
  • tag cloud generation
  • related tags handling
  • unit-tested
  • machine tags support (also called "triple tags")
  • easy tags fixtures loading

Philosophy of the stuff

  • taggable models must have a primary key
  • tags are saved when the object is saved, not before
  • one object cannot be tagged twice with the same tag. When trying to use twice the same tag on one object, the second tagging will be ignored
  • the tags associated to one taggable object are only loaded when necessary. Then they are cached.
  • once created, tags never change in the Tag table. When using replaceTag(), a new tag is created if necessary, but the old one is not deleted.

Get it installed

  • go to your project's root

  • Install the plugin:

     ./symfony plugin-install http://plugins.symfony-project.com/sfPropelActAsTaggableBehaviorPlugin
  • if not already done, enabled behaviors in config/propel.ini:

     propel.builder.addBehaviors = true
  • edit the classes that you want to make taggable. For instance, for lib/model/Post.php:

    [php] addTag('toto'); $post->addTag('tata, tutu'); $post->addTag(array('Titi', 'Gros Minet')); $post->save(); Since the version 0.4, the plugin supports [machine tags](http://www.flickr.com/groups/api/discuss/72157594497877875/): [php] addTag('iso:isbn=123456789'); $post->save(); // assume City is a taggable class $city = new City(); $city->addTag('geo:lat=47.3456'); $city->save(); ### Retrieving one object's tags It is possible to retrieve tags from a taggable object: [php] getTags(); foreach ($tags as $tag) { echo $tag.'
    '; }

Removing one object's tags

Of course, tags can also be removed:

$post = PostPeer::retrieveByPk(1);
$post->removeTag('toto, tutu');

Setting one object's tags

All the tags of an object can be set or replaced at once, using the methode setTags():

$post = PostPeer::retrieveByPk(1);
$post->setTags('toto, tutu');

This is particularly useful when using File Syntax fixtures in a project, as it permits to attach tags to the objects a pretty straight way:

    title:    My first memories
    tags:     memories, sleeping, bed

    title:    Things got worse
    tags:     death, memories, personnal

Retrieving objects, based on their tags

The plugin proposes several methods for retrieving objects given their tags. These methods are all located in the TagPeer class:

// gets the list of the models that have at least one instance tagged with one 
// or several specific tags
$tutu_toto_models = TagPeer::getModelsTaggedWith('tutu, toto');
// gets objects tagged with one or several specific tags
$tutu_toto_objects = TagPeer::getTaggedWith('tutu, toto');true
$tutu_toto_objects = TagPeer::getTaggedWith('tutu, toto', array('triple' => true, 'namespace' => 'geo'));
$tutu_toto_objects = TagPeer::getTaggedWith('tutu, toto', array('model' => 'Post'));
// gets a criteria that permits to select objects tagged with one or several 
// specific tags
$criteria = TagPeer::getTaggedWithCriteria('Post', 'tutu, toto', array('model' => 'Post'));
$criteria->add(PostPeer::PUBLISHED, true);
$posts = PostPeer::doSelect($c);

Tags cloud generation

The plugin also proposes methods and helpers for generating tags cloud:

// gets the popular tags
$tags = TagPeer::getPopulars();
// display the tags cloud. The tags will use the route name "@tag" which tags 
// the request parameter "tags"
echo tag_cloud($tags, '@tag?tags=');

The default size of the tag cloud is 100 items, but this value might be tweaked in app.yml:

    limit:   50

When you click on a tag in a tag cloud, you will want to get a list of objects that have been tagged with that tag. But sometimes, it happens that this tag is so popular that you can not find the resource you were searching for. Related-tags clouds are helpful for refining your request, as they provide a way to add an other tag to the request:

// get the tags related to "toto" and "tutu", for the model "Post" only
$tags = TagPeer::getRelatedTags('toto,tutu', array('model' => 'Post'));
// displays the related tags cloud, using the route "@post_tags" with the 
// request parameter "tags"
echo related_tag_cloud($tags, '@post_tags?tags=', 'toto,tutu');

Specialize your tag clouds

The tag retrieval mecanism is fully based on Criterias, so it is easy to pass several restrictions. For instance, for retrieving popular tags over posts created in March 2007:

$c = new Criteria();
$c->addJoin(PostPeer::ID, TaggingPeer::TAGGABLE_ID);
$c->add(PostPeer::CREATED_AT, '2007-03%', Criteria::LIKE);
$tags = TagPeer::getPopulars($c, array('model' => 'Post'));
echo tag_cloud($tags, '@tag?tags=');

The methods TagPeer::getPopulars, TagPeer::getAll, etc., accept as last parameter an array with several keys: * max number of returned tags:

// return a maximum of 200 tags
$tags = TagPeer::getAll(null, array('limit' => 200));
  • tag name restriction:

    [php] 'to%'));

  • whether the returned tags should be machine tags, or not:

    [php] true));

  • for triple tags, it is possible to restrict the returned tags from their namespace, key, and value:

    [php] true, 'namespace' => 'geo'));

    // returns only triple tags with teh key "lat" $lat_tags = TagPeer::getAll(null, array('triple' => true, 'key' => 'lat'));

    // returns only triple tags with teh value "12" $value_tags = TagPeer::getAll(null, array('triple' => true, 'value' => '12'));

Avoid performance problems

In case you want to display a long list of taggable objects with their associated tags, you might want first to preload these objects's tags: it avoids to load tags per object, and gets all tags in a few requests.

$posts = TagPeer::getTaggedWith('toto,tutu', array('model' => 'Post'));
foreach ($posts as $post)
  echo $post-getTitle();
  // won't require one request at each loop, as tags have been preloaded.

Plugin internals

The plugin associates a parameterHolder to Propel objects, with 3 disjoin namespaces: * tags: tags that have been attached to the object, but not yet saved. Contract: tags are disjoin of (saved_tags union removed_tags) * saved_tags: tags that are presently saved in the database. Contract: removed_tags are disjoin of (tags union saved_tags) * removed_tags: tags that are presently saved in the database, but which will be removed at the next save(). Contract: removed_tags are disjoin of (tags union saved_tags)

When required, the saved_tags namespace is filled with the tags previously present in the database. The tagging methods have an action on these three namespaces, which are serialized in the database after the Propel object gets saved.

What is done when adding a tag ?

  • if the tag is present in the "removed_tags" namespace, the tagging request is interpreted as a tag-removal revert. The tag is then deleted front the "removed_tags" request, and brought back into "saved_tags".
  • else, if the tag is not present in the "saved_tags" namespace, add it to the "tags" one.

What is done when removing a tag ?

  • if the tag has not yet been saved, simply remove it from the "tags" namespace.
  • if he has been saved, remove it from the "saved_tags" namespace, and add it to the "removed_tags" one.


The behavior implement the following methods: * addTag($tagname) - Adds one or several tags to an object. * getTags() - Returns the list of the tags attached to the object. * hasTag($tag = null) - Returns true if the object has a tag. If a tag ar an array of tags is passed in second parameter, checks if these tags are attached to the object. * removeAllTags() - Removes all the tags of the object. * removeTag($tagname) - Removes a tag or a set of tags from the object. * replaceTag($tagname, $replacement = null) - Replaces a tag with an other one. * setTags($tagname) - Sets teh tags of the object. If previous tags were attached to the object, they are removed.

The behavior class also implement the following method, which is a facility for preloading all the tags for a set of taggable objects * preloadTags($objects) - Preload tags for a set of objects.

Unit testing

The plugin has been deeply unit-tested, if not fully. The tests are located in test/unit/sfPropelActAsTaggableBehaviorTest.php. If you want to run them: * install the plugin * configure a model for using it, for instance "Post" * copy the test file to /path/to/your/project/test/unit/sfPropelActAsTaggableBehaviorTest.php * edit this file and modify line 3:

     define('TEST_CLASS', 'Post');
  • run the tests:

     ./symfony test-unit sfPropelActAsTaggableBehavior

License and credits

This plugin has been developed by Xavier Lacot and is licensed under the MIT license. Thanks to Tristan Rivoallan for the help provided.


version 0.5 - 2008-03-21

  • compatibility with Symfony 1.1
  • added setTags() method
  • renamed table names in order to use the sf_ prefix

version 0.4 - 2007-12-11

  • added machine tags support (also called "triple tags") (thanks to Michael Nolan)

version 0.3 - 2007-07-02

  • fixed bug in tags removal (thanks Alexander Alexandrov)

version 0.2 - 2007-06-27

  • indexes on tags model
  • bugfixes
  • thanks to Nicolas Perriault for his useful comments

version 0.1 - 2007-05-22

Initial public release. Features tags attachment to heterogene Propel objects, and includes tag-clouds generation.