S
S
soho3502020-02-04 22:03:57
symfony
soho350, 2020-02-04 22:03:57

How correctly to organize record in a DB, the field invited?

How to properly write to the database. There is a field who invited during registration. It should display a list of names of already registered users. There is a User entity, it has first name and last name parameters. If you create another entity who invited, then which connection to choose? Who did not understand, then the problem is in the field who invited. Help a noob).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2020-02-04
@soho350

There is no need for another entity.
One-To-Many, Self-referencing
You can also set up a one-to-many association that is self-referencing. In this example we set up a hierarchy of Category objects by creating a self referencing relationship. This effectively models a hierarchy of categories and from the database perspective is known as an adjacency list approach.

class User
{
    /**
     * Кого пригласил
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="User", mappedBy="invitedBy")
     */
    private $invitedMembers;

    /**
     * Кем был приглашен
     * @var User
     * @ORM\ManyToOne(targetEntity="User", inversedBy="invitedMembers")
     */
    private $invitedBy;

    public function __construct() {
        $this->invitedMembers = new \Doctrine\Common\Collections\ArrayCollection();
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question