Skip to content
Advertisement

Symfony Imageboard post database schema

I’m making imageboard in symfony. I am curios if I’m doing it in the best way.

There are threads and posts just like in normal forum.

  • Thread 1

    • post 1
    • post 3
  • Thread 2

    • post 2
    • post 4

The problem is I made two entities Thread and Post. It’s easy to get all posts from one Thread but there are some problems

  • I can’t make >>id link for thread and post at the same time. the thread has another id numeration from the post. I can make another link system for example >>t>id but I think the same id numeration for both is better for the user.
  • doubled SQL table schema – each post and thread has title, content, date, image, etc.
  • fronted js functions need another parameter to know if its post or thread

and I think there will be more problems. Maybe I should use only post without thread because it’s the same except thread_id.

Symfony won’t allow making relation in one table. I can write it as a number but doctrine will be doing multiple queries which is much worse.

What should I do?

Advertisement

Answer

I found the answer: https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/association-mapping.html#many-to-many-self-referencing

    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     * @OneToMany(targetEntity="Post", mappedBy="parent")
     */
    private $id;

    /**
     * @ORMManyToOne(targetEntity=Post::class, inversedBy="children", fetch="EAGER")
     * @ORMJoinColumn(name="parent_id", referencedColumnName="id")
     */
    private $parent;

    /**
     * @ORMOneToMany(targetEntity=Post::class, mappedBy="parent", fetch="LAZY")
     */
    private $children;
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement