Symfony1 plugin to implement materialized path repository for storing hierarchical data
- Symfony 1.4
- Propel ORM Plugin 1.6
- PHP 5.4+
- Add requirement to your
composer.json
:
"require": {
"e1stuff/axis-materialized-path-repository-plugin": "0.1.*"
}
- Build model by running
php symfony propel:build-model
- Update your database schema (using propel migration or manually)
- Enable plugin in
ProjectConfigiration
Assume you have entity in your schema that need to be stored hierarchically:
propel:
document:
id: ~
title: ~
created_at: ~
author_id: { type: int }
You do not need to modify your schema to organize your objects into tree.
Implement getTreeId()
and getTreeType()
methods in your entity:
class Document
{
public function getTreeId()
{
return (string)$this->getPrimaryKey();
}
public function getTreeType()
{
return __CLASS__;
}
}
Tree control:
// create a tree manager
$tree = new TreeManager('Document'); // Document - your entity name
$root = new Document();
$root->setTitle('Homepage');
$root->save(); // note that entity should have ID before putting into
$tree->put('/', $root);
$contacts = new Document();
$contacts->setTitle('Contact us');
$contacts->save();
$tree->put('/contact-us/', $contacts);
$about = new Document();
$about->setTitle('About us');
$about->save();
$tree->put('/about-us/');
// ...
$doc = $tree->get('/contact-us/');
echo $doc->getTitle(); // Contact us
$children = $tree->getChildren('/'); // [$contacts, $about]
$tree->move('/about/', '/our-company/', Position::before($contacts));
echo $tree->get('/our-company/')->getTitle(); // About us
$children = $tree->getChildren('/'); // [$about, $contacts]
See public methods of TreeManager class.
put($path, $entity)
- put an entity link into treemove($path, $newPath, $position = null)
- move entity link to another empty locationreorder($path, $position)
- reorder entity link without changing hierarchyget($path)
- get entity associated with pathremove($path)
- remove pathgetAll()
- return all tree entitiesgetBranch($path)
- returns all entities of a subtree (including subroot) (map [path => entity])getSiblings($path)
- return all entities within same parent (map [path => entity])countSiblings($path)
- return number of siblingsgetChildren($path)
- return all direct children (map [path => entity])countChildren($path)
- return number of direct childrengetAncestors($path)
- return all ancestors (map [path => entity])getLevel($path)
- return level of pathgetDescendants($path)
- return all descendants (map [path => entity])countDescendants($path)
- return number of all descendants