Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
/**
* @ORM\ManyToOne(targetEntity="Menu", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="Menu", mappedBy="parent")
*/
private $children;
/**
* Constructor
*/
public function __construct()
{
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set parent
*
* @param \Common\MenuBundle\Entity\Menu $parent
* @return Menu
*/
public function setParent(\Common\MenuBundle\Entity\Menu $parent = null)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* @return \Common\MenuBundle\Entity\Menu
*/
public function getParent()
{
return $this->parent;
}
/**
* Add children
*
* @param \Common\MenuBundle\Entity\Menu $children
* @return Menu
*/
public function addChildren(\Common\MenuBundle\Entity\Menu $children)
{
$this->children[] = $children;
return $this;
}
/**
* Remove children
*
* @param \Common\MenuBundle\Entity\Menu $children
*/
public function removeChildren(\Common\MenuBundle\Entity\Menu $children)
{
$this->children->removeElement($children);
}
/**
* Get children
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getChildren()
{
return $this->children;
}
public function mainMenu(FactoryInterface $factory, array $options)
{
$menuItems = $this->container->get('menu')->getMainMenu();
$menu = $factory->createItem('root');
$this->setCurrentItem($menu);
$menu->setChildrenAttribute('class', 'nav nav-justified');
$menu->setExtra('currentElement', 'active');
foreach($menuItems as $item) {
if ($item->getParent()) {
$em = $this->container->get('doctrine')->getEntityManager();
$parent = $em->getRepository('CommonMenuBundle:menu')->find($item->getParent());
$menu[$parent->getTitle()]
->setChildrenAttribute('class', 'dropdown-menu dropdown-menu-large row')
->setAttribute("class", "dropdown dropdown-large")
->addChild($item->getTitle(), array('uri' => $item->getRoute()))
->setAttribute("class", "col-sm-6")
;
} else {
$menu->addChild($item->getTitle(), array('uri' => $item->getRoute()));
}
}
return $menu;
}


KnpMenuBundle + Sonata. Делаем меню из базы