Micchaleq
Micchaleq

Reputation: 433

Tree menu in Symfony 2, in one entity

I'm using Symfony 2.3 framework.

I am trying to create a system that will allow me to easily create tree menu (with root and child).

This field allows to show whether it is a root or child (id of this entity another record).

Now I want to do something like relation OneToMany but I don't know how. It is possibility to create relation with this same entity?

I thing about create method 'getChildren' in this entity but I don't know what operation I should to do.

Example:

class site { private $id; private $name; private $parent; }

and records in db: 1 category_1 0 2 subcategory_1 1 3 subcategory_2 1 etc.

Upvotes: 1

Views: 2071

Answers (1)

m0c
m0c

Reputation: 2191

I think there is even an example in the doctrine documentation: http://docs.doctrine-project.org/en/latest/reference/association-mapping.html#many-to-many-self-referencing

But if you really want to build a Tree you could also consider the TreeExtension which uses a slightly different database model, which is a little bit more performant: https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/tree.md

Upvotes: 1

Related Questions