csDoctrineActAsCategorizablePlugin
1.4.0stable
for sf 1.4sf 1.3 MIT
Add Nested Categories to your models. Models can be grouped into individual categories (this is the default), or multiple models can share a group of categories.
Sponsored By Centre{source} interactive agency
Developers
License
Copyright (c) 2009 Brent Shaffer
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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.