Self related Category table (infinite nested array)

Dear phalcon community,

I need a help with this. I have a category table with such structure:

'id', 'name', 'parent_id'

The model:

class Category extends Model
{
public $id;
public $name;
public $parent_id;

public function initialize() {
$this->hasMany("id", 'Category', "parent_id", ['alias' => 'children']);
$this->belongsTo("parent_id", 'Category', "id", ['alias' => 'parent']);
}

public function getSource()
{
return 'category';
}

public function getCategories($parameters = null)
{
return $this->getRelated('children', $parameters);
}

}

I am trying to get all children of each of category in model with function getCategories. But unfortunately it shows error:

Model 'Category' could not be loaded

Can someone help me? How to make a self relation with parent_id?

And how you are getting those categories ? Beacause i don't see Specialization anywhere.

And how you are getting those categories ? Beacause i don't see Specialization anywhere. Oh, I am sorry - I misstyped. Now it is the right error.

Did you have Category in any namespace ? If yes then you need to provide full namespace in relations.

Perhaps my answer here could help you: https://forum.phalconphp.com/discussion/11195/get-all-related-records-children-and-grandchildren#C32677

The result will be array of full category tree:


array(
 'parent' => '',
 'sub' => array(
  'parent' => '',
  'sub' => array(
      // e.t.c. ....
  )
 )
)