37 lines
1.5 KiB
PHP
37 lines
1.5 KiB
PHP
|
|
|
|
Managing tree structures in doctrine is easy. Doctrine currently fully supports Nested Set, and plans to support the other implementations soon. To set your model to act as a tree, simply add the code below to your models table definition.
|
|
|
|
|
|
|
|
Now that Doctrine knows that this model acts as a tree, it will automatically add any required columns for your chosen implementation, so you do not need to set any tree specific columns within your table definition.
|
|
|
|
|
|
|
|
Doctrine has standard interface's for managing tree's, that are used by all the implementations. Every record in the table represents a node within the tree (the table), so doctrine provides two interfaces, Tree and Node.
|
|
|
|
|
|
|
|
<code type="php">
|
|
class Menu extends Doctrine_Record {
|
|
public function setTableDefinition() {
|
|
|
|
$this->setTableName('menu');
|
|
|
|
// add this your table definition to set the table as NestedSet tree implementation
|
|
// $implName is 'NestedSet' or 'AdjacencyList' or 'MaterializedPath'
|
|
// $options is an assoc array of options, see implementation docs for options
|
|
$this->option('treeImpl', $implName);
|
|
$this->option('treeOptions', $options);
|
|
|
|
// you do not need to add any columns specific to the nested set implementation, these are added for you
|
|
$this->hasColumn("name","string",30);
|
|
|
|
}
|
|
|
|
// this __toString() function is used to get the name for the path, see node::getPath()
|
|
public function __toString() {
|
|
return $this->get('name');
|
|
}
|
|
}
|
|
</code> |