- PHP 5.4
- This plugin needs li3_behaviors.
- This plugin needs li3_fixtures (only if you intend to run tests).
Checkout the code to either of your library directories:
cd libraries
git clone [email protected]:jails/li3_tree
Include the library in your /app/config/bootstrap/libraries.php
Libraries::add('li3_tree');
This behavior store hierarchical datas in a database table using the MPTT logic.
To use the tree behavior, your table needs the following 3 extra fields:
- The
'parent'
config field. By default the field must be namedparent_id
in the table. - The
'left'
config field. By default the field must be namedlft
in the table. - The
'right'
config field. By default the field must be namedrght
in the table.
Example of attaching the tree behavior to a model:
<?php
//app/models/Comments.php
namespace app\models;
use li3_behaviors\data\model\Behaviors;
class Comments extends \lithium\data\Model {
use Behaviors;
public $belongsTo = ['Posts'];
protected $_actsAs = ['Tree' => ['scope' => ['post_id']]];
}
?>
In order to store multiple trees in the same table you need to set the 'scope'
option. For example, if a Post
hasMany Comment
and a Comment
belongsTo a Post
. To allow multiple Comment
trees in the same table, you must scope Comment
by the foreign key post_id
. This way all Comment
trees will be independant.
'scope'
can be a full condition:
protected $_actsAs = ['Tree' => ['scope' => ['region' => 'head']]];
Or a simple fieldname like a foreign key:
protected $_actsAs = ['Tree' => ['scope' => ['post_id']]];
In this last case, the full condition will be populated from entity datas. This mean you can't do any CRUD action if the entity datas don't contain all necessary datas for perfoming a well scoped CRUD action.
<?php
$root1 = Comment::create(['post_id' => 1]);
$root1->save();
$root2 = Comment::create(['post_id' => 2]);
$root2->save();
$neighbor1 = Comment::create(['post_id' => 1]);
$neighbor1->save();
$neighbor1->moveDown();
$root1->moveUp();
$neighbor1->move(0);
$subelement1 = Comment::create(['post_id' => 1, 'parent_id' => $neighbor1->id]);
$subelement1->save();
var_export($root1->childrens());
var_export($subelement1->path());
?>
The li3 team, Vogan and all others which make that possible (I mean only because Chuck Norris agreed).