Using the TreeBehavior in CakePHP 3.9 you can easily create a formatted list:
$list = $categories->find('treeList');
My Categories
_Fun
__Sport
___Surfing
___Skating
_Trips
__National
__International
Can the reverse be done too? I let my users modify a tree list (like the example above) and I want to save the result as new tree.
Is there a built in feature to create a new tree with existing data, either as formatted text or cascading entities?
The alternative for me would be to process the list and save the entities one by one, and let the TreeBehavior work out the lft
and rght
columns.
There is no such built-in functionality, no, you'll have to take care of that yourself.
You kinda have two options, either save the nodes one by one, and let the behavior update the lft
/rght
fields on each save, or disable the behavior while saving, and recover the tree afterwards, possibly using a scope in case you have multiple trees.
In any case you need to populate the parent_id
field for your records accordingly when saving, without that field the tree behavior will not be able to generate the proper lft
/rght
field values.