Many-to-many relationship on the same table

431 views Asked by At

I try get all related records from many-to-many table.

I have two tables:

  1. Table post has id and title column.
  2. Table post2post has parent_id and child_id column.

post2post have many-to-many relationship on the same table post.

For example, if post id=1 is the parent post id=2 and post id=3

parent_id   child_id
1           2
1           3

And when I select post id=3, I should have post id=1 and post id=2

Here is my DQL query

         SELECT p as post
         FROM AppBundle:Post p
         WHERE p in (
            SELECT parent
            FROM AppBundle:Post p2
            LEFT JOIN p2.parentPost as parent
            WHERE p2 = (:post)
         )
         OR p in(
            SELECT child
            FROM AppBundle:Post p3
            LEFT JOIN p3.childPost as child
            WHERE p3 = (:post)
        )

I know, this query does not work properly, but it's all I could do

0

There are 0 answers