![]() |
|
sfPropelActAsNestedSetBehaviorPlugin - 0.7.0Propel nested set behavior |
|
Some of the plugins offer one-click modules to easily add complete features to your symfony applications
![]() |
DescriptionThe sfPropelActAsNestedSetBehaviorPlugin is a symfony plugin that provides nested set capabilities to Propel objects. Categories |
| Name | |
|---|---|
|
|
ten.nallaovir <<ta>> natsirt |
| Version | License | API | Released |
|---|---|---|---|
| 0.9.1beta | LGPL | 0.9.1beta | 23/07/2007 |
| 0.9.0beta | LGPL | 0.9.0beta | 24/05/2007 |
| Version | License | API | Released |
|---|---|---|---|
| 0.9.1beta | LGPL | 0.9.1beta | 23/07/2007 |
| 0.9.0beta | LGPL | 0.9.0beta | 24/05/2007 |
| Version | License | API | Released |
|---|---|---|---|
| 0.9.1beta | LGPL | 0.9.1beta | 23/07/2007 |
| 0.9.0beta | LGPL | 0.9.0beta | 24/05/2007 |
| 0.8.2beta | MIT license | 0.8.2beta | 18/04/2007 |
| 0.8.1beta | MIT license | 0.8.1beta | 22/03/2007 |
| 0.8.0beta | MIT license | 0.8.0beta | 19/02/2007 |
| 0.7.0beta | MIT license | 0.7.0beta | 19/02/2007 |
| 0.6.2beta | MIT license | 0.6.2beta | 19/02/2007 |
| 0.6.1beta | MIT license | 0.6.1beta | 15/02/2007 |
| 0.6.0beta | MIT license | 0.6.0beta | 15/02/2007 |
| 0.5.1beta | MIT license | 0.5.1beta | 14/02/2007 |
| 0.5.0beta | MIT license | 0.5.0beta | 14/02/2007 |
Copyright (c) 2007 Tristan Rivoallan
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.
Go to the ticketing system to view opened tickets or report a bug.
php symfony plugin-install http://plugins.symfony-project.org/sfPropelActAsNestedSetBehaviorPlugin
Go to the repository: http://svn.symfony-project.com/plugins/sfPropelActAsNestedSetBehaviorPlugin
The sfPropelActAsNestedSetBehaviorPlugin is a symfony plugin that provides nested set capabilities to Propel objects.
Nested sets (aka modified preorder tree traversal) is a very efficient way (in terms of performances) to browse and edit a tree like structure in an RDBMS.
You can read a good introduction to nested sets on MySQL developers' zone.
Install the plugin
symfony plugin-install http://plugins.symfony-project.com/sfPropelActAsNestedSetBehaviorPlugin
Add new fields to your schema.xml
<column name="tree_left" type="INTEGER" required="true" />
<column name="tree_right" type="INTEGER" required="true" />
<column name="tree_parent" type="INTEGER" required="true" />
<column name="topic_id" type="INTEGER" required="true" />
Enable Propel behavior support in propel.ini:
propel.builder.AddBehaviors = true
If you have to enable the behavior support, rebuild your model:
symfony propel-build-model
Enable the behavior for one of your Propel model:
// lib/model/ForumPost.php
class ForumPost
{
}
$columns_map = array('left' => ForumPostPeer::TREE_LEFT,
'right' => ForumPostPeer::TREE_RIGHT,
'parent' => ForumPostPeer::TREE_PARENT,
'scope' => ForumPostPeer::TOPIC_ID);
sfPropelBehavior::add('ForumPost', array('actasnestedset' => array('columns' => $columns_map)));
The column map is used by the behavior to know which columns hold information it needs :
$root = new ForumPost(); $root->makeRoot(); $root->save(); $p1 = new ForumPost(); $p1->insertAsFirstChildOf($root); $p1->save(); $p2 = new ForumPost(); $p2->insertAsFirstChildOf($p1); $p2->save(); /* * Resulting tree : * * ROOT * |- P1 * |- P2 */
$root1 = new ForumPost(); $root1->makeRoot(); $root1->setTopicId(1); $root1->save(); $root2 = new ForumPost(); $root2->makeRoot(); $root2->setTopicId(2); $root2->save(); $p1 = new ForumPost(); $p1->insertAsFirstChildOf($root1); $p1->save(); $p2 = new ForumPost(); $p2->insertAsFirstChildOf($root2); $p2->save(); /* * Resulting trees : * * ROOT1 * |- P1 * * ROOT2 * |- P2 */
$root = ForumPostPeer::retrieveByPk(rootnodepk);
<ul>
<?php echo $root->getTitle() ?>
<?php foreach ($root->getDescendants() as $post): ?>
<li style="padding-left: <?php echo $post->getLevel() ?>em;">
<?php echo $post->getTitle() ?>
</li>
<?php endforeach; ?>
</ul>
// Decide which posts to fetch
$c = new Criteria();
$c->add(ForumPostPeer::TOPIC_ID, $topic_id);
$c->addAscendingOrderByColumn(ForumPostPeer::TREE_LEFT); // ForumPostPeer::TREE_LEFT is the column holding nested set's left value
// Create pager
$pager = new sfPropelPager('ForumPost', 10);
$pager->setCriteria($c);
$pager->setPage($this->getRequestParameter('page', 1));
$pager->init();
Enabling the behaviors adds the following method to the Propel objects :
void insertAsFirstChildOf(BaseObject $dest_node) : Inserts node as first child of given node.void insertAsLastChildOf(BaseObject $dest_node) : Inserts node as last child of given node.void insertAsNextSiblingOf(BaseObject $dest_node) : Inserts node as next sibling of given node.void insertAsPrevSiblingOf(BaseObject $dest_node) : Inserts node as previous sibling of given node.bool hasChildren() : Returns true if given node as one or several children.bool isRoot() : Returns true if given node is a root node.bool hasParent() : Returns true if given node has a parent node.bool hasNextSibling() : Returns true if given node has a next sibling.bool hasPrevSibling() : Returns true if given node has a previous sibling.bool isLeaf() : Returns true if given node does not have children.integer getNumberOfChildren() : Returns given node number of direct children.integer getNumberOfDescendants() : Returns given node number of descendants (n level).integer getLevel() : Returns given node level.array getChildren($peer_method = 'doSelect') : Returns given node direct children.array getDescendants($peer_method = 'doSelect') : Returns given node descendants (n level).BaseObject retrieveNextSibling() : Returns given node next sibling.BaseObject retrievePrevSibling() : Returns given node previous sibling.BaseObject retrieveFirstChild() : Returns given node first child.BaseObject retrieveLastChild() : Returns given node last child.BaseObject retrieveParent() : Returns given node parent.array getPath() : Returns path to a specific node as an array, useful to create breadcrumbs.void moveToFirstChildOf(BaseObject $dest_node) : Moves node to first child of given node.void moveToLastChildOf(BaseObject $dest_node) : Moves node to last child of given node.void moveToNextSiblingOf(BaseObject $dest_node) : Moves node to next sibling of given node.void moveToPrevSiblingOf(BaseObject $dest_node) : Moves node to previous sibling of given node.void deleteChildren() : Deletes node direct childrenvoid deleteDescendants() : Deletes node descendants (n level)void makeRoot() : Sets node properties to make it a root node.BaseObject reload() : Returns an up to date version of nodePublic API completed : implemented remaining missing methods (+ unit tests) :
moveToPrevSiblingOfmoveToNextSiblingOfdeleteChildrendeleteDescendantsFixed a bug due to wrong usage of rtrim. (Thanks to Krešo Kunjas)
Fixed minor bug in getPath()
Implemented missing node retrieval methods :
retrieveFirstChildretrieveLastChildretrieveParentgetPathUpdated docs and unit tests accordingly
Pear package missed plugin's config.php file.
Initial public release. The behavior is stable and fully unit-tested, but the API is not yet complete. Missing methods :
retrieveFirstChildretrieveLastChildmoveToPrevSiblingOfmoveToNextSiblingOfdeleteChildrendeleteTreegetPath
