1
I’m using Doctrine, and now I have the following problem.
I am making a register of menus system, where the same can have several "children" and their "children" can also have "children", and so on.
How will I realize this self-relationship with Doctrine?
I have already performed the mapping in the Menu class with the Annotations, but I could not understand how I will do this mapping in the query, so that when performing the search the menu object is already filled with all the items correctly.
Below is the mapping I performed 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();
}
}
So Rodrigo I already read this article and I did the relationship as they explained, but I still don’t understand how I’m going to build my query to return the data. If you have an example there and can pass me I thank you.
– André Cavalhieri
I edited the answer by citing an extension to Doctrine to create structures similar to what you need. I use it here for the same purpose. See if it fits :)
– Rodrigo Rigotti