CakePHP tree behaviour not working (what I am doing wrong?)



I am learning cakePHP 3 and trying to store hierarchical data in a table. I have added parent_id, lft and rght columns. Added $this->addBehavior(‘Tree’) in initialize method for this table. Calling recover method.

So seems all is done as described here: But no one child row is returned.

That is interesting, that childCount returns -0.5 for each row (no matter if the row has children or not, always -0.5).

I am newbie in cakePHP, so maybe someone can help, what I am doing wrong?


Without code, nobody can see what you’re doing wrong :slight_smile:


This is my categories model:

namespace App\Model\Table;

use Cake\ORM\Table;

class CategoriesTable extends Table
public function initialize(array $config)

And this is categories controller (beginning):
public function categories(){
$categories = TableRegistry::get(‘Categories’);

$children = $categories
->find(‘children’, [‘for’ => 203])

foreach ($children as $child) {
echo “blablabla”;

Categories table:
CREATE TABLE categories (
categoryID int(11) NOT NULL,
categoryTitle varchar(255) NOT NULL DEFAULT ‘’,
parent_id int(11) NOT NULL DEFAULT ‘0’,
categoryPosition int(11) NOT NULL DEFAULT ‘10’,
categoryURL varchar(255) DEFAULT NULL,
categoryHeaderText varchar(255) NOT NULL,
categoryPageTitle varchar(255) NOT NULL,
categoryPageDescription varchar(255) NOT NULL,
categoryMetaTags varchar(255) NOT NULL,
categoryIsDefaultForDomain int(11) NOT NULL,
lft int(11) NOT NULL,
rght int(11) NOT NULL


I suppose that recover() is not working for some reason. Because every row has 0 in lft and rght columns


I have checked it, when I manually added lft and rght - everything is Ok.
Does anybody know why recover may fail? I could add all lfts and rghts manually, but it is a bad decision, and my table has too many rows:)