Self-relationship doctrine

1

I'm using Doctrine, and now I have the following problem.

I'm doing a system registry, where it can have multiple "children" and their "children" can also have "children", and so on.

How will I achieve this self-relationship with the doctrine?

I have already mapped the Menu class to the annotations , but I could not figure out how to do this mapping in the query, so when I perform the search the menu object will already be filled with all the items correctly.

Below is the mapping I made in the Menu class:

/**
 * @ORM\Entity
 * @ORM\Table(name="tb_Menu")
 */
class Menu
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue("SEQUENCE")
     * @ORM\Column(type="integer", nullable=false)
     * @ORM\SequenceGenerator(sequenceName="sq_Menu")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Menu", inversedBy="menuFilho")
     * @ORM\JoinColumn(name="idMenuPai", referencedColumnName="id")
     */
    private $menuPai;

    /**
     * @ORM\OneToMany(targetEntity="Menu", mappedBy="menuPai")
     */
    private $menuFilho;

    /**
     * @ORM\Column(type="string", length=30, unique=true, nullable=false)
     */
    private $titulo;

    /** 
     * @ORM\Column(type="string", length=80)
     */
    private $descricao;

    /* 
     * Construtor
     */
    public function __construct() {
        $this->menuFilho = new ArrayCollection();
    }
}
    
asked by anonymous 13.04.2015 / 15:17

1 answer

1

What you need to do is create a One-To-Many Relationship Self-Referencing .

Read this section in the Doctrine documentation:

p>

Basically, you will use a single table to construct the tree of your menu, and the rows of the child nodes will relate to their respective parent nodes through the same table.

In addition, there is a very interesting extension for Doctrine called Tree . With this extension you can build tree-like structures in your database (for example, your category and sub-categories) and still render HTML on top of that structure. See if it suits you.

    
13.04.2015 / 15:56