symfonyknpmenubundle

Argument is nullable and no null value has been provided


I'm using KnpMenuBundle to generate childs of my menu.

For each category, I have subcategory. I passed the 2 parameters in my method (in the controller as mentioned below).

The principle menu of categories works well but when I click on one of the sub cat I get this error :

enter image description here

public function souscategorieAction(string $cat,string $subcat,Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $query1 = 'SELECT p.*,(SELECT min(h.prix) FROM Histprix h,Urlproduit u
                WHERE h.urlproduit_id=u.id and u.produit_id=p.id  ) as "prixmin",(SELECT max(h.prix) FROM Histprix h,Urlproduit u
                WHERE h.urlproduit_id=u.id and u.produit_id=p.id  ) as "prixmax", (select count(*) from produit_revendeur  where produit_id=p.id) as "offre"
            FROM  Produit p,Subcat sub,Categorie cat
            WHERE p.status=1 and p.subcat_id=sub.id and sub.categorie_id=cat.id and and cat.nom like'.$cat.' and sub.nom like'.$subcat.';';


    $statement = $em->getConnection()->prepare($query1);
    $statement->execute();
    $produit = $statement->fetchAll();

    $paginator=$this->get('knp_paginator');
    $produits=$pagination= $paginator->paginate (
            $produit,
            $request->query->getInt('page',1),
            $request->query->getInt('limit',5));


    return $this->render('ProductBundle:Product:mobilepage.html.twig', array('produits'=>$produits,'souscategorie'=>$subcat ,'categorie'=>$cat));

This is my routing:

souscat:
    path:     /{categorie}/{souscategorie}
    defaults: { _controller: ProductBundle:Product:souscategorie } 

And this is my MenuBuilder :

public function createMainMenu(array $options) {
        $c = "";
        $s = "";
        $menu = $this->factory->createItem('root');
        $menu->addChild('Accueil', array('route' => 'home'));

        $categories = $this->em->getRepository('ProductBundle:Categorie')->findAll();
        $subcats = $this->em->getRepository('ProductBundle:Subcat')->findAll();
foreach ($categories as $c) {


            switch ($c) {
                case "Telephonie":
                    $menu->addChild($c, array('route' => 'telephonie'));
                    break;
                case "Informatique":
                    $menu->addChild($c, array('route' => 'informatique'));

                    break;
                case "TV SON Vidéo":
                    $menu->addChild($c, array('route' => 'tvsonvideo'));

                    break;
                case "Electroménager":
                    $menu->addChild($c, array('route' => 'electromenager'));

                    break;
                case "Impression":
                    $menu->addChild($c, array('route' => 'impression'));

                    break;
            }
            foreach ($subcats as $s) {
                if ($c->getNom() == $s->getCategorie()->getNom())

                    $menu[$c->getNom()]->addChild($s->getNom(), ['route' => 'souscat', 'routeParameters' => array('categorie' => $c->getNom(), 'souscategorie' => $s->getNom())]);
            }
        }



    $menu->addChild('Contact', array('route' => 'contactus'));



    return $menu;
}

Solution

  • Can you try to rename your function parameters with the same name as your variables ?

    From

    public function souscategorieAction(string $cat,string $subcat,Request $request)
    

    To

    public function souscategorieAction(string $categorie,string $souscategorie,Request $request)