[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/AkActiveRecord/AkActsAsBehaviours/ -> AkActsAsTree.php (summary)

(no description)

Author: Niels Ganser
Copyright: Copyright (c) 2006, Raw Ideas Pty Ltd
License: GNU Lesser General Public License
File Size: 376 lines (14 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 1 file
 AkActiveRecord/AkObserver.php

Defines 1 class

AkActsAsTree:: (24 methods):
  AkActsAsTree()
  init()
  _ensureIsActiveRecordInstance()
  reloadActiveRecordInstance()
  getType()
  getScopeCondition()
  setScopeCondition()
  getScopedColumn()
  getParentColumnName()
  setParentColumnName()
  getDependent()
  setDependent()
  hasChildren()
  hasParent()
  addChild()
  childrenCount()
  getChildren()
  getParent()
  getAncestors()
  getSiblings()
  getSelfAndSiblings()
  getDescendants()
  _recursiveGetDescendants()
  beforeDestroy()


Class: AkActsAsTree  - X-Ref

acts_as_tree

Makes your model acts as a tree (surprise!). Consider the following example:

class Category extends ActiveRecord {
var $acts_as = 'tree';
}

$Category = new Category;

$CategoryA = $Category->create();
$CategoryAa = $Category->create();
$CategoryAa1 = $Category->create();
$CategoryAa2 = $Category->create();
$CategoryAb = $Category->create();
$CategoryB = $Category->create();

$CategoryA->tree->addChild($CategoryAa)
$CategoryA->tree->addChild($CategoryAb)
$CategoryAa->tree->addChild($CategoryAa1)
$CategoryAa->tree->addChild($CategoryAa2)


This will effectively give you:

Category A
\_ Category Aa
\_ Category Aa1
\_ Category Aa2
\_ Category Ab
Category B


OK. Admittedly you won't get a graph in real life. But at least the following functions:

$CategoryA->tree->hasChildren()        # ==> true
$CategoryA->tree->childrenCount()    # ==> 2
$CategoryA->tree->getChildren()         # ==> array($CategoryAa, $CategoryAb)
// fairly expensive operation follows
// (yes, array(parent, array_of_children) is not nice but unfortunately PHP doesn't allow for objects as keys)
$CategoryA->tree->getDescendants()    # ==> array(array($CategoryAa, array($CategoryAa1, $CategoryAa2)), $CategoryAb)

$CategoryAa->tree->getChildren()        # ==> array($CategoryAa1, $CategoryAa2)
$CategoryAa->tree->getSiblings()        # ==> array($CategoryAb)
$CategoryAa->tree->hasParent()        # ==> true
$CategoryAa->tree->getParent()        # ==> $CategoryA

$CatagoryAa1->tree->hasChildren()    # ==> false
$CategoryAa1->tree->getParent()        # ==> $CategoryAa
// fairly expensive operation follows
$CategoryAa1->tree->getAncestors()    # ==> array($CategoryAa, $CategoryA)
// fairly expensive operation follows
$CategoryAa1->tree->getAncestors(1)    # ==> array($CategoryAa)


To make this work your model needs a parent_id column (whose name can be overriden with +parent_column+. Furthermore
you can set the +dependent+ option to automatically delete all children if their parent gets deleted. Otherwise they
will become orphants (i.e. have parent_id = NULL)

(Note that on adding a child it will be saved. If the parent has been unsaved until now it will also be saved.)
AkActsAsTree(&$ActiveRecordInstance)   X-Ref
No description

init($options = array()   X-Ref
No description

_ensureIsActiveRecordInstance(&$ActiveRecordInstance)   X-Ref
No description

reloadActiveRecordInstance(&$nodeInstance)   X-Ref
No description

getType()   X-Ref
No description

getScopeCondition()   X-Ref
No description

setScopeCondition($scope_condition)   X-Ref
No description

getScopedColumn($column)   X-Ref
No description

getParentColumnName()   X-Ref
No description

setParentColumnName($parent_column_name)   X-Ref
No description

getDependent()   X-Ref
No description

setDependent($val)   X-Ref
No description

hasChildren()   X-Ref
No description

hasParent()   X-Ref
No description

addChild( &$child )   X-Ref
No description

childrenCount()   X-Ref
No description

getChildren()   X-Ref
No description

getParent()   X-Ref
No description

getAncestors($level=0)   X-Ref

param: integer    $level    How deep do you want to search? everything <= 0 means infinite deep

getSiblings($options = array()   X-Ref
No description

getSelfAndSiblings()   X-Ref
No description

getDescendants($level=0)   X-Ref

param: integer    $level    How deep do you want to search? everything <= 0 means infinite deep

_recursiveGetDescendants($level, $from)   X-Ref
No description

beforeDestroy(&$object)   X-Ref
No description



Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6