csDoctrineActAsCategorizablePlugin - 1.4.0

A plugin that adds categories to your model

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Releases Changelog Contribute
Show source

sfDoctrineActAsCategorizablePlugin

Introduction

This behavior permits to assign categories to doctrine models.

WARNING: this is the documentation for the sf1.4 branch. The plugins changed a lot since the previous version. You can also read the doc for the symfony 1.1 version

Installation

You can install this plugin as usual. You can also checkout the last developement version, if you're feeling lucky.

Assuming your SCM is git:

$ git submodule add -b sf1.4 git://github.com/thibault/csDoctrineActAsCategorizablePlugin.git plugins/csDoctrineActAsCategorizablePlugin

Features

Also note that:

  • Categorizable models must have a primary key
  • Categories are saved when the object is saved

 How to use

This plugins will add a Category class to your schema. It act as a nested set, read the doctrine documentation to know how to handle this.

$category = new Category();
$category->name = 'Cooking';
$category->save();
 
$treeObject = Doctrine_Core::getTable('Category')->getTree();
$treeObject->createRoot($category);

To make objects from a class categorizable, just activate the behavior in your schema, rebuild your model, and you're ready to go.

Article:
  actAs:
    Categorizable: ~

From now, you can simply add or delete categories, the same way you would do with any other hasMany relation.

$article = new Article();
$article->setTitle('My cooking article');
$article->Categories[] = $category;
$article->save();

Be careful, categories are actually stored in a variable called _categories in the record, because of a technical limitation.

print_r($article->toArray());
 
Array
(
 [id] => 1
 [title] => my cooking article
 [_categories] => Array
 (
  [0] => Array
  (
   [id] => 1
   [name] => category1
   [root_id] => 1
   [lft] => 1
   [rgt] => 4
   [level] => 0
  )
 )
)

So if you want to use the fromArray method to hydrate or update a record, you have to use the above format.

Category Methods

// returns the assigned parent category (excluding the category tree root)
$category->getParentCategory()
 
// returns a collection of sub categories for the category
$category->getSubCategories()
 
// sets the $child category as a direct descendent of $category
$category->setSubCategory($child*)
 
// sets the $category category as a direct descendent of $parent
$category->setParentCategory($parent*)
 
// gets a collection of objects of the given class who belongs to this category
$articles = $category->getObjects('Article');
 
// Same methods, but returns all objects for the whole subtree
$articles = $category->getSubtreeObjects('Article');
 
__ *variable is mixed, can be a string or a Category __

Category Table Methods

// returns a Category object with the specified name
$category = Doctrine::getTable('Category')->getCategory($name);
 
// Same method, but create the category if it doesn't exists
$category = Doctrine::getTable('Category')->getOrCreateCategory($name);
 
// Get all categories in the category subtree (root included)
$categories = Doctrine::getTable('Category')->getSubTreeCategories('Cooking');

Testing the plugin

This plugins comes with an auto-generated (with the sfTaskExtraPlugin) fixtures project, to run the tests without touching your own app. Tests are not included in the package version, so you'll have to checkout the trunk version.

Note that those tests are written to be run with lime 2 (alpha1).

You don't have to configure any database, as we provide an sqlite one. Just make sure the php-sqlite extensio is installed.

Here's how you can easily run the plugin test suite :

$ php plugins/csDoctrineActAsCategorizablePlugin/test/bin/prove.php

Professional support

The 1.4 branch of this plugin is developped by Thibault Jouannic. I'm a freelance developer, working from France, and can provide professional support for this plugin. Contact me at thibault << at >> jouannic.fr, I speak french and english.