You must first sign up to be able to contribute.

Version 2 (modified by Grégory Becker, 11 years ago)

This code may be moved to a pear package later. Let's just be sure there aren't bugs in it anymore first.

sfPropelTree plugin

Create in file with the following path : /lib/plugins/sfPropelTree/sfPropelTree.class.php
Paste the content from[[BR]] Syntax highlighted

Clear the cache for symfony to find this new plugin.

symfony cc


Now, add two columns to the table that will store the tree.
You cannot change the names of theses two columns.

<table ....>
    <column name="lft" type="integer" required="true" />
    <column name="rgt" type="integer" required="true" />

Then rebuild DB structure

symfony propel-build-sql
symfony propel-build-model

And don't forget to apply table modification to mysql.


Add this methods to your class. This will keep the class clean and will make shortcuts to the real functions in sfPropelTree.


class MyTable extends BaseMyTable {
  // sfPropelTree Functions. Do not modify unless you know what you are doing
  function save($con = null)      { sfPropelTree::saveNode($this); parent::save($con); }
  function delete($con = null)    { sfPropelTree::deleteNode($this); parent::delete($con); }
  function deleteChilds()         { return sfPropelTree::deleteChilds($this); }
  function addChild($child)       { return sfPropelTree::addChildBottom($this, $child); }
  function addChildTop($child)    { return sfPropelTree::addChildTop($this, $child); }
  function addChildBottom($child) { return sfPropelTree::addChildBottom($this, $child); }
  function addLeft($child)        { return sfPropelTree::addBefore($this, $child); }
  function addBefore($child)      { return sfPropelTree::addBefore($this, $child); }
  function addRight($child)       { return sfPropelTree::addAfter($this, $child); }
  function addAfter($child)       { return sfPropelTree::addAfter($this, $child); }
  function moveBefore($refNode)   { return sfPropelTree::moveBefore($refNode, $this); }
  function moveAfter($refNode)    { return sfPropelTree::moveAfter($refNode, $this); }
  function hasChilds()            { return sfPropelTree::hasChilds($this); }
  function getChilds()            { return sfPropelTree::getChilds($this); }
  function getFirstChild()        { return sfPropelTree::getFirstChild($this); }
  function getLastChild()         { return sfPropelTree::getLastChild($this); }
  function getParent()            { return sfPropelTree::getParent($this); }
  function getRoot()              { return sfPropelTree::getRoot($this); }
  function getPath()              { return sfPropelTree::getPath($this); }
  function getLeft()              { return sfPropelTree::getLeft($this); }
  function getRight()             { return sfPropelTree::getRight($this); }



Do the same, but for MyTablePeer?.class.php this time.


class MyTablePeer extends BaseMyTablePeer {
  // sfPropelTree Functions. Do not modify unless you know what you are doing
  static function getIdField ()   { return self::ID; }
  static function getLftField ()  { return self::LFT; }
  static function getRgtField ()  { return self::RGT; }
  static function getTable()      { return self::TABLE_NAME; }
  static function getRoot()       { return sfPropelTree::getRoot(__CLASS__); }
  static function print_r()       { return sfPropelTree::print_r(__CLASS__); } // debug function


Examples of use

In the following examples, I have setup a table named Page.
Each page can contains subpages.


<?xml version="1.0" encoding="UTF-8"?>
<database name="propel" defaultIdMethod="native" noxsd="true">
  <table name="page" phpName="Page">
    <column name="id" type="integer" required="true" primaryKey="true" autoincrement="true" />
    <column name="title" type="varchar" size="255" required="true" />
    <column name="lft" type="integer" required="true" />
    <column name="rgt" type="integer" required="true" />

adding a node

  public function executeAddNode() {
    $page = new Page();
    $page->setTitle('First page');

That's it.
So what happens ? You just added a root page (a page which has no parent)