The following permit to easily load tree datas with propel-load-data command
We have the following class :
config/schema.yml (using sfPropelAlternativeSchemaBehavior) :
classes:
Family:
columns:
name: varchar(255)
tree_scope: integer
tree_parent: { type: integer, foreignClass: MyTreeClass, foreignReference: id, onDelete: cascade }
tree_left: integer
tree_right: integer
behaviors:
actasbested:
columns:
scope: tree_scope
parent: tree_parent
left: tree_left
right: tree_right
And we want to create this tree :
Arnold
________|______
| |
Bob Emilie
____|_____
| |
Clara Dave
So we just define our datas like this
fixtures/test_data.yml :
Familly:
A:
name: Arnold
B:
name: Bob
tree_parent: A
C:
name: Clara
tree_parent: B
D:
name: Dave
tree_parent: B
E:
name: Emilie
tree_parent: A
And the command symfony propel-build-all-load myapp will do the rest thanks to this piece of code
replacement of sfPropelActAsNestedBehavior::preSave function :
public function preSave(BaseObject $node)
{
// If left or right value are missing, there is a problem !
if ( !($node->getLeftValue() and $node->getRightValue()) )
{
// If parent id is set, we will correct the values of left and right
if ( $node->getParentIdValue() )
{
$parent_node = call_user_func( array( get_class($node->getPeer()), 'retrieveByPk' ), $node->getParentIdValue() );
$node->insertAsLastChildOf( $parent_node );
}
// If no parent is set, let's make it at least root!
else
{
$node->makeRoot();
}
}
$this->processPreSaveStack();
}