[英]Symfony 2 - Trying to get the properties from another method in the same class
As you can see in the code below, I am attempting to make a sub-navigation. 如您在下面的代码中看到的,我正在尝试进行子导航。 I want to make it from scratch, rather than use a bundle because I'm using this as a learning exercise. 我想从头开始,而不是使用捆绑软件,因为我将其用作学习练习。 I've reached a point where I think I've just about cracked it. 我认为我已经快要破解了。 All I need now is to access the results of the navAction() db query inside subNavAction(). 我现在需要的是访问subNavAction()中的navAction()数据库查询的结果。
I've managed to get as far as 'echo self::navAction($sector);' 我已经设法达到'echo self :: navAction($ sector);' which DOES echo out the full navigation again but I can't seem to access individual items in the query. 确实再次呼应了整个导航,但是我似乎无法访问查询中的单个项目。 What I actually need to do is to loop through the results of navAction() and get the results for all items with children. 我真正需要做的是遍历navAction()的结果,并获取所有带有子项的结果。
I am really new to OOPHP (although I know procedural quite well) and Symfony/Doctrine so the more comprehensive and answer you can give me, the better. 我对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
)
);
}
It's not the best solution,but you can create an object variable $this->navItems; 这不是最好的解决方案,但是您可以创建一个对象变量$ 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
)
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.