Bonjour,
Je viens vers vous pour trouver la raison d'une erreur dont je ne comprend pas la raison.
Pour un projet personnel j'utilise KnpMenuBundle et le Nested set de SoftExtensionDoctrine pour créer mon menu à l'aide d'un service.
Lorsque je créer mon service pour afficher le Menu non dynamique (ajout manuelle des routes et enfants) il fonctionne mais quand j'essaye de le créer via la méthode createFromNode($node) de KnpMenuBundle pour qu'il soit dynamique selon la base de donnée il m'affiche cette erreur :
FatalErrorException: Error: Class Front\AccueilBundle\Entity\Menu contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (Knp\Menu\NodeInterface::getName, Knp\Menu\NodeInterface::getOptions) in C:\wamp\www\portfolio-symfony\Symfony\src\Front\AccueilBundle\Entity\Menu.php line 251
Je comprend pas ce message car dans mon Entity j'ai bien implémenté Knp\Menu\NodeInterface donc je doit bien pouvoir utiliser les différente méthodes non?
voici mes différent fichier :
menuBuilder.php
<?php
namespace Front\AccueilBundle\Menu;
use Symfony\Component\DependencyInjection\ContainerAware;
use Doctrine\ORM\EntityManager;
use Knp\Menu\FactoryInterface;
class MenuBuilder extends ContainerAware
{
private $factory;
private $em;
/**
* @param FactoryInterface $factory
*/
public function __construct(FactoryInterface $factory, \Doctrine\ORM\EntityManager $em)
{
$this->factory = $factory;
$this->em = $em;
}
public function createMainMenu(Request $request)
{
/*
Test du menu manuelle (fonctionnel)
$menu = $this->factory->createItem('root');
$menu->addChild('Home', array('route' => 'acceuil_homepage'));
return $menu
*/
// récupère le Repository
$repo = $this->em->getRepository('AcceuilBundle:Menu');
// récupère la route dont le titre est Food
$food = $repo->findOneByTitle('Food');
// récupère les enfant de la route récupérée avant
$node = $repo->children($food);
/* $htmlTree = $repo->childrenHierarchy();*/
/* \Doctrine\Common\Util\Debug::dump($htmlTree);*/
$menu = $this->factory->createFromNode($node);
return $menu;
}
}
Menu.php :
<?php
namespace Front\AccueilBundle\Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Knp\Menu\NodeInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* @Gedmo\Tree(type="nested")
* @ORM\Table(name="Menu")
* use repository for handy tree functions
* @ORM\Entity(repositoryClass="Front\AcceuilBundle\Entity\Repository\MenuRepository")
*/
class Menu implements NodeInterface
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
private $id;
/**
* @ORM\Column(name="title", type="string", length=64)
*/
private $title;
/**
* @var text $description
* @ORM\Column(name="description", type="text", nullable=true)
*/
private $description;
/**
* @Gedmo\TreeLeft
* @ORM\Column(name="lft", type="integer")
*/
private $lft;
/**
* @Gedmo\TreeLevel
* @ORM\Column(name="lvl", type="integer")
*/
private $lvl;
/**
* @Gedmo\TreeRight
* @ORM\Column(name="rgt", type="integer")
*/
private $rgt;
/**
* @Gedmo\TreeRoot
* @ORM\Column(name="root", type="integer", nullable=true)
*/
private $root;
/**
* @Gedmo\TreeParent
* @ORM\ManyToOne(targetEntity="Menu", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="Menu", mappedBy="parent")
* @ORM\OrderBy({"lft" = "ASC"})
*/
private $children;
public function getId()
{
return $this->id;
}
public function setTitle($title)
{
$this->title = $title;
}
public function getTitle()
{
return $this->title;
}
public function setParent(Menu $parent = null)
{
$this->parent = $parent;
}
public function getParent()
{
return $this->parent;
}
/**
* @ORM\PrePersist
* Constructor
*/
public function __construct()
{
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Set lft
*
* @param integer $lft
* @return Menu
*/
public function setLft($lft)
{
$this->lft = $lft;
return $this;
}
/**
* Get lft
*
* @return integer
*/
public function getLft()
{
return $this->lft;
}
/**
* Set lvl
*
* @param integer $lvl
* @return Menu
*/
public function setLvl($lvl)
{
$this->lvl = $lvl;
return $this;
}
/**
* Get lvl
*
* @return integer
*/
public function getLvl()
{
return $this->lvl;
}
/**
* Set rgt
*
* @param integer $rgt
* @return Menu
*/
public function setRgt($rgt)
{
$this->rgt = $rgt;
return $this;
}
/**
* Get rgt
*
* @return integer
*/
public function getRgt()
{
return $this->rgt;
}
/**
* Set root
*
* @param integer $root
* @return Menu
*/
public function setRoot($root)
{
$this->root = $root;
return $this;
}
/**
* Get root
*
* @return integer
*/
public function getRoot()
{
return $this->root;
}
/**
* Add children
*
* @param \Front\AcceuilBundle\Entity\Menu $children
* @return Menu
*/
public function addChild(\Front\AcceuilBundle\Entity\Menu $children)
{
$this->children] = $children;
return $this;
}
/**
* Remove children
*
* @param \Front\AcceuilBundle\Entity\Menu $children
*/
public function removeChild(\Front\AcceuilBundle\Entity\Menu $children)
{
$this->children->removeElement($children);
}
/**
* Get children
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getChildren()
{
return $this->children;
}
/**
* Set description
*
* @param string $description
* @return Menu
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
}
service.yml :
parameters:
# acceuil.example.class: Front\AccueilBundle\Example
services:
# acceuil.example:
# class: %acceuil.example.class%
# arguments: @service_id, "plain_value", %parameter%]
# src/Acme/MainBundle/Resources/config/services.yml
front_main.menu_builder:
class: Front\AccueilBundle\Menu\MenuBuilder
arguments: "@knp_menu.factory", "@doctrine.orm.entity_manager"]
front_main.menu.main:
class: Knp\Menu\MenuItem # the service definition requires setting the class
factory_service: front_main.menu_builder
factory_method: createMainMenu
arguments: "@request"]
scope: request # needed as we have the request as a dependency here
tags:
- { name: knp_menu.menu, alias: main } # The alias is what is used to retrieve the menu
index.html.twig :
{% extends '::base.html.twig' %}
{% block body %}
{{ knp_menu_render('main') }}
{% endblock %}
Je vous remercie d'avance pour votre aide