1
1
1alexandr2015-03-09 08:36:04
Doctrine ORM
1alexandr, 2015-03-09 08:36:04

How to implement a many-to-many relationship for the same table?

Hello dear experts!
I'm having a problem implementing a multi-level directory. There is a category entity, it has id, parent_id, name. The parent category does not know anything about the children. The question is, how do you introduce a parent to a child?
Nothing came to my mind except a many-to-many relationship. But how to implement this and is it even possible?
Sorry for the noob question.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Abdula Magomedov, 2015-03-09
@Avarskiy

What? Display all children whose parent is equal to the id of the parent, so you have a connection between the "parent" and the "daughters" ???
For a many-to-many relationship, create another table. In which there will be fields `parent` and `child`.
Display all children by parent id:
Display all parents by child id:
Naturally, the table should have an `id` field for convenient manipulation with it.

I
index0h, 2015-03-09
@index0h

category: [id, name]
categoryHasCategory: [parentId, childId]
Just be careful, this schema is vandal-resistant and may contain recursive references.
Still better for trees is ONE-TO-MANY
category: [id, parentId, name]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question