0

正如您在下面的代码中看到的那样,我正在尝试进行子导航。我想从头开始制作它,而不是使用捆绑包,因为我将其用作学习练习。我已经到了一个我认为我刚刚破解它的地步。我现在需要的是访问 subNavAction() 中的 navAction() db 查询的结果。

我已经设法达到'echo self::navAction($sector);' 它确实再次回显了完整的导航,但我似乎无法访问查询中的各个项目。我真正需要做的是循环遍历 navAction() 的结果并获取所有有子项的结果。

我对 OOPHP(虽然我非常了解程序)和 Symfony/Doctrine 真的很陌生,所以你能给我的答案越全面越好。

//Render Navigation
public function navAction($sector)
{
    $em = $this->getDoctrine()->getManager();
    $query = $em->createQuery(
       "SELECT DISTINCT p.pageName, p.pageParent
        FROM styleGuideBundle:pages p 
        WHERE  p.pageSector LIKE :sector 
        AND p.pageType = 2"
    );
    $query->setParameter(':sector', '%'.$sector.'%');
    $navItems = $query->getResult();
    if (!$navItems) throw $this->createNotFoundException('Unable to find any pages under this category');
    return $this->render(
        'styleGuideBundle:Partial:peNav.html.twig',
        array(
            'navItems' => $navItems,
            'sector' => $sector
        )
    );
}

//Render SubNavigation
public function subnavAction($sector)
{
    echo self::navAction($sector); // This line is where I got stuck.
    $em = $this->getDoctrine()->getManager();
    $query = $em->createQuery(
       "SELECT DISTINCT p.pageName, p.pageParent
        FROM styleGuideBundle:pages p 
        WHERE  p.pageSector LIKE :sector 
        AND p.pageType = 2"
    );
    $query->setParameter(':sector', '%'.$sector.'%');
    $navItems = $query->getResult();
    if (!$navItems) throw $this->createNotFoundException('Unable to find any pages under this category');
    return $this->render(
        'styleGuideBundle:Partial:peNav.html.twig',
        array(
            'navItems' => $navItems,
            'sector' => $sector
        )
    );
}
4

1 回答 1

1

这不是最好的解决方案,但您可以创建一个对象变量 $this->navItems;

PageController extends Controller {

      public $navItems = null; 

    //Render Navigation
        public function navAction($sector)
        {
            $em = $this->getDoctrine()->getManager();
            $query = $em->createQuery(
               "SELECT DISTINCT p.pageName, p.pageParent
                FROM styleGuideBundle:pages p 
                WHERE  p.pageSector LIKE :sector 
                AND p.pageType = 2"
            );
            $query->setParameter(':sector', '%'.$sector.'%');
            $this->navItems = $query->getResult();

            if (!$this->navItems) throw $this->createNotFoundException('Unable to find any pages under this category');
            return $this->render(
                'styleGuideBundle:Partial:peNav.html.twig',
                array(
                    'navItems' => $this->navItems,
                    'sector' => $sector
                )
            );
        }


        //Render SubNavigation
        public function subnavAction($sector)
        {
            $this->navAction($sector);
            var_dump($this->navItems);

            $em = $this->getDoctrine()->getManager();
            $query = $em->createQuery(
               "SELECT DISTINCT p.pageName, p.pageParent
                FROM styleGuideBundle:pages p 
                WHERE  p.pageSector LIKE :sector 
                AND p.pageType = 2"
            );
            $query->setParameter(':sector', '%'.$sector.'%');
            $navItems = $query->getResult();
            if (!$navItems) throw $this->createNotFoundException('Unable to find any pages under this category');
            return $this->render(
                'styleGuideBundle:Partial:peNav.html.twig',
                array(
                    'navItems' => $navItems,
                    'sector' => $sector
                )
            );
}
于 2013-09-09T09:35:51.343 回答