0

我已经使用 knpmenubundle 构建了一个菜单,它运行得非常好,并且可以从数据库中动态提取导航。然而,客户现在想向它添加一个子菜单。我编写了一个控制器来查找子页面,但是我看不到如何将它们附加到正确父页面下的子导航。目前它所做的一切是将当前元素的副本作为子节点附加到自身。

这是菜单生成器:

 $sector = $options['sector'];
    $em = $this->getDoctrine()->getManager();
    $navItems = $this->getDoctrine()
    ->getRepository('acmeStyleGuideBundle:pageSector')
    ->findBySectorJoinedToUrl($sector);
    if (!$navItems) throw $this->createNotFoundException('Unable to find any matching pages');
    $menu = $factory->createItem('root');
    foreach ($navItems as $nav)
    {
        // Doesn't have any parents, This is a top level
        $parent = $menu->addChild($nav->getPageName(), array(
            'route' => 'acme_style_guide_pages_show',
            'routeParameters' => array(
                'sector' => $sector,
                'pageUrl' => $nav->getPageUrl()
            )
        ));
        $parent->addChild($nav->getPageName(), array(
            'route' => 'acme_style_guide_pages_show_sub',
            'routeParameters' => array(
                'sector' => $sector,
                'pageParent' => $nav->getPageName(),
                'pageUrl' => $nav->getPageUrl()
            ))
        );
    }

这是父元素的控制器:

 // Render pages from database
public function showAction($sector, $pageUrl)
{
    $em = $this->getDoctrine()->getManager();
    $pages = $this->getDoctrine()
    ->getRepository('acmeStyleGuideBundle:PageContent')
    ->findByUrlJoinedToSector($sector, $pageUrl);

    if (!$pages) throw $this->createNotFoundException('Unable to find any matching pages');
    return $this->render(
        'acmeStyleGuideBundle:Page:pages.html.twig', 
        array(
            'Pages' => $pages,
            'sector' => $sector
        )
    );
}

这是子元素的控制器:

// Render sub pages from database
public function showSubAction($sector, $pageParent, $pageUrl)
{
    $em = $this->getDoctrine()->getManager();
    $pages = $this->getDoctrine()
    ->getRepository('acmeStyleGuideBundle:PageContent')
    ->findByUrlJoinedToSectorAndParent($sector, $pageParent, $pageUrl);

    if (!$pages) throw $this->createNotFoundException('Unable to find any matching pages');
    return $this->render(
        'acmeStyleGuideBundle:Page:pages.html.twig', 
        array(
            'Pages' => $pages,
            'sector' => $sector
        )
    );
}

这两个控制器都使用自定义存储库类从数据库中提取信息。

这是自定义存储库代码:

public function findByUrlJoinedToSector($sector, $pageUrl)
{
    $query = $this->getEntityManager()
        ->createQuery('
            SELECT p, s FROM acmeStyleGuideBundle:PageContent p
            JOIN p.pageSector s
            LEFT JOIN p.pageTypes t
            WHERE p.pageUrl = :url
            AND s.sectorName = :sector
            AND t.typeName != :type'
        )
        ->setParameter('url', $pageUrl)
        ->setParameter('sector', $sector)
        ->setParameter('type', 'Section Headers');

    try {
        return $query->getResult();
    } catch (\Doctrine\ORM\NoResultException $e) {
        return null;
    }
}

public function findByUrlJoinedToSectorAndParent($sector, $pageParent, $pageUrl)
{
    $query = $this->getEntityManager()
        ->createQuery('
            SELECT p, s, c FROM acmeStyleGuideBundle:PageContent p
            JOIN p.pageSector s
            LEFT JOIN p.pageTypes t
            LEFT JOIN p.PageParent c
            WHERE p.pageUrl = :url
            AND s.sectorName = :sector
            AND c.pageUrl = :parent
            AND t.typeName != :type'
        )
        ->setParameter('url', $pageUrl)
        ->setParameter('sector', $sector)
        ->setParameter('parent', $pageParent)
        ->setParameter('type', 'Section Headers');

    try {
        return $query->getResult();
    } catch (\Doctrine\ORM\NoResultException $e) {
        return null;
    }
}

这可能只是周一早上的问题,所以我会继续努力自己解决,如果我这样做了,我会在这里发布它作为答案。

附加信息

更新的构建器 // 找到所有带有父母的项目。然后将找到的项目附加到其父项。$subNavItems = $this->getDoctrine() ->getRepository('acmeStyleGuideBundle:pageSector') ->findSubNav($sector, $nav->getPageParent());

        // loop over them and add a link for each one:
        foreach ($subNavItems as $child) {
            if ($child->getPageParent() != NULL) {
                $parent->addChild($child->getPageName(), array(
                    'route' => 'acme_style_guide_pages_show_sub',
                    'routeParameters' => array(
                        'sector' => $sector,
                        'pageParent' => $child->getPageName(),
                        'pageUrl' => $child->getPageUrl()
                        )
                    )
                );
            }
        }
    }

它正在输出:

<ul>
    <li><a href="link">Target Item</a></li>
    <li><a href="link">Item</a></li>
    <li><a href="link">Item</a></li>
    <li><a href="link">Child</a>
        <ul>
            <li><a href="link">Child</a></li>
        </ul>
    </li>
</ul>

而我需要它输出:

<ul>
    <li><a href="link">Target Item</a>
        <ul>
            <li><a href="link">Child</a></li>
        </ul>
    </li>
    <li><a href="link">Item</a></li>
    <li><a href="link">Item</a></li>
    <li><a href="link">Child</a></li>
</ul>

这是 PageContent 表的架构

<database name="styleguide">
        <!-- Table PageContent -->
        <table name="PageContent">
            <column name="id">1</column>
            <column name="pageTypesId">2</column>
            <column name="pageName">Text Elements</column>
            <column name="pageUrl">text-elements</column>
            <column name="richText">&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&lt;/p&gt;&lt;p&gt;Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.&lt;/p&gt;</column>
            <column name="PageParent">NULL</column>
        </table>
        <table name="PageContent">
            <column name="id">2</column>
            <column name="pageTypesId">2</column>
            <column name="pageName">Buttons</column>
            <column name="pageUrl">buttons</column>
            <column name="richText">&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&lt;/p&gt;&lt;p&gt;Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.&lt;/p&gt;</column>
            <column name="PageParent">NULL</column>
        </table>
        <table name="PageContent">
            <column name="id">3</column>
            <column name="pageTypesId">2</column>
            <column name="pageName">Forms</column>
            <column name="pageUrl">forms</column>
            <column name="richText">&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&lt;/p&gt;&lt;p&gt;Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.&lt;/p&gt;</column>
            <column name="PageParent">NULL</column>
        </table>
        <table name="PageContent">
            <column name="id">4</column>
            <column name="pageTypesId">2</column>
            <column name="pageName">Lists</column>
            <column name="pageUrl">lists</column>
            <column name="richText">&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&lt;/p&gt;&lt;p&gt;Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.&lt;/p&gt;</column>
            <column name="PageParent">NULL</column>
        </table>
        <table name="PageContent">
            <column name="id">5</column>
            <column name="pageTypesId">2</column>
            <column name="pageName">Tables</column>
            <column name="pageUrl">tables</column>
            <column name="richText">&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&lt;/p&gt;&lt;p&gt;Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.&lt;/p&gt;</column>
            <column name="PageParent">NULL</column>
        </table>
        <table name="PageContent">
            <column name="id">6</column>
            <column name="pageTypesId">2</column>
            <column name="pageName">Search Box</column>
            <column name="pageUrl">search-box</column>
            <column name="richText">&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&lt;/p&gt;&lt;p&gt;Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.&lt;/p&gt;</column>
            <column name="PageParent">NULL</column>
        </table>
        <table name="PageContent">
            <column name="id">7</column>
            <column name="pageTypesId">2</column>
            <column name="pageName">Pods</column>
            <column name="pageUrl">pods</column>
            <column name="richText">&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&lt;/p&gt;&lt;p&gt;Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.&lt;/p&gt;</column>
            <column name="PageParent">NULL</column>
        </table>
        <table name="PageContent">
            <column name="id">8</column>
            <column name="pageTypesId">3</column>
            <column name="pageName">Business</column>
            <column name="pageUrl">business</column>
            <column name="richText">&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&lt;/p&gt;&lt;p&gt;Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.&lt;/p&gt;</column>
            <column name="PageParent">NULL</column>
        </table>
        <table name="PageContent">
            <column name="id">9</column>
            <column name="pageTypesId">3</column>
            <column name="pageName">Residential</column>
            <column name="pageUrl">residential</column>
            <column name="richText">&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&lt;/p&gt;&lt;p&gt;Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.&lt;/p&gt;</column>
            <column name="PageParent">NULL</column>
        </table>
        <table name="PageContent">
            <column name="id">10</column>
            <column name="pageTypesId">1</column>
            <column name="pageName">Paragraphs</column>
            <column name="pageUrl">paragraphs</column>
            <column name="richText">&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.&lt;/p&gt;&lt;p&gt;Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.&lt;/p&gt;</column>
            <column name="PageParent">1</column>
        </table>
    </database>
4

1 回答 1

1

您需要遍历所有subElements内容并为每个内容呈现导航链接:

foreach ($navItems as $nav)
{
    // Doesn't have any parents, This is a top level
    $parent = $menu->addChild($nav->getPageName(), array(
        'route' => 'acme_style_guide_pages_show',
        'routeParameters' => array(
            'sector' => $sector,
            'pageUrl' => $nav->getPageUrl()
        )
    ));

    // find all sub-items:
    $subNavItems = $this->getDoctrine()
    ->getRepository('acmeStyleGuideBundle:pageSector')
    ->findByUrlJoinedToSectorAndParent($sector, $parent, /* $pageUrl */);

    // loop over them and add a link for each one:
    foreach ($subNavItems as $child) {
        $parent->addChild($child->getPageName(), array(
            'route' => 'acme_style_guide_pages_show_sub',
            'routeParameters' => array(
                'sector' => $sector,
                'pageParent' => $child->getPageName(),
                'pageUrl' => $child->getPageUrl()
            ))
        );
    }
}
于 2013-09-23T09:40:56.480 回答