我试图在Symfony3中构建一个类别树。
我有以下内容:
类别实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
* @ORM\Table(name="category")
*/
class Category
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
private $id;
/**
* @ORM\Column(type="string")
*/
private $name;
/**
* @ORM\Column(type="string", unique=true)
*/
private $slug;
/**
* One Category has Many Categories.
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Category", mappedBy="parent")
*/
private $children;
/**
* Many Categories have One Category.
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="children")
*/
private $parent;
/**
* @ORM\Column(type="string")
*/
private $pageTitle;
/**
* @ORM\Column(type="text")
*/
private $description;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User")
* @ORM\JoinColumn(name="created_by", referencedColumnName="id")
*/
private $created_by;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return mixed
*/
public function getSlug()
{
return $this->slug;
}
/**
* @param mixed $slug
*/
public function setSlug($slug)
{
$this->slug = $slug;
}
/**
* @return mixed
*/
public function getParent()
{
return $this->parent;
}
/**
* @param mixed $parent
*/
public function setParent($parent)
{
$this->parent = $parent;
}
/**
* @return mixed
*/
public function getPageTitle()
{
return $this->pageTitle;
}
/**
* @param mixed $pageTitle
*/
public function setPageTitle($pageTitle)
{
$this->pageTitle = $pageTitle;
}
/**
* @return mixed
*/
public function getDescription()
{
return $this->description;
}
/**
* @param mixed $description
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* @return mixed
*/
public function getChildren()
{
return $this->children;
}
/**
* @param mixed $children
*/
public function setChildren($children)
{
$this->children = $children;
}
/**
* @return mixed
*/
public function getCreatedBy()
{
return $this->created_by;
}
/**
* @param mixed $created_by
*/
public function setCreatedBy($created_by)
{
$this->created_by = $created_by;
}
public function __toString()
{
return (string) $this->getName();
}
}
CategoryRepository:
<?php
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
class CategoryRepository extends EntityRepository
{
public function findAllParentCategories()
{
return $this->createQueryBuilder('category')
->where('category.parent IS NULL')
->getQuery()
->execute();
}
}
构建树的代码:
/**
* @Route("/category")
*/
public function createCategoryMenu()
{
$categoryRepository = $this->getDoctrine()->getRepository('AppBundle:Category');
$categories = $categoryRepository->findAllParentCategories();
$test = $this->generateCategoryMenu($categories, '');
echo $test;
exit;
}
protected function generateCategoryMenu($categories, $tree) {
$tree .= "<ul>";
foreach($categories as $category) {
$tree .= "<li>" . $category->getName();
if($category->getChildren() != null) {
$tree .= $this->generateCategoryMenu($category->getChildren(), $tree);
}
$tree .= "</li>";
}
$tree .= "</ul>";
return $tree;
}
在我的脑海中,这应该有效,我无法弄清楚它为什么不起作用。我得到以下结果:
<ul><li>Hiking<ul><li>Hiking<ul><li>Backpacks<ul><li>Hiking<ul><li>Backpacks<ul></ul></li><li>Shoes<ul><li>Hiking<ul><li>Backpacks<ul><li>Hiking<ul><li>Backpacks<ul></ul></li><li>Shoes<ul><li>Men<ul><li>Hiking<ul><li>Backpacks<ul><li>Hiking<ul><li>Backpacks<ul></ul></li><li>Shoes<ul><li>Men<ul></ul></li></ul></li></ul></li><li>Survival<ul><li>Hiking<ul><li>Hiking<ul><li>Backpacks<ul><li>Hiking<ul><li>Backpacks<ul></ul></li><li>Shoes<ul><li>Hiking<ul><li>Backpacks<ul><li>Hiking<ul><li>Backpacks<ul></ul></li><li>Shoes<ul><li>Men<ul><li>Hiking<ul><li>Backpacks<ul><li>Hiking<ul><li>Backpacks<ul></ul></li><li>Shoes<ul><li>Men<ul></ul></li></ul></li></ul></li><li>Survival<ul><li>Axes<ul><li>Hiking<ul><li>Hiking<ul><li>Backpacks<ul><li>Hiking<ul><li>Backpacks<ul></ul></li><li>Shoes<ul><li>Hiking<ul><li>Backpacks<ul><li>Hiking<ul><li>Backpacks<ul></ul></li><li>Shoes<ul><li>Men<ul><li>Hiking<ul><li>Backpacks<ul><li>Hiking<ul><li>Backpacks<ul></ul></li><li>Shoes<ul><li>Men<ul></ul></li></ul></li></ul></li><li>Survival<ul><li>Axes<ul></ul></li></ul></li></ul>
&#13;
我期待以下结果:
<ul><li>Hiking<ul><li>Backpacks<ul></ul></li><li>Shoes<ul><li>Men<ul></ul></li></ul></li></ul></li><li>Survival<ul><li>Axes<ul></ul></li></ul></li></ul>
&#13;
如您所见,我想要的<ul>
是较大<ul>
的结尾。为什么要渲染所有重复的<li>
- 标签?
答案 0 :(得分:1)
问题(和解决方案)非常简单。在递归调用中,您传入已部分构建的树:
$tree .= $this->generateCategoryMenu($category->getChildren(), $tree);
// ^--------------------------------------------------------------^
然后将结果连接到调用函数中的树。此时有效地复制整个树。
解决方案:简单地不要传递树。然后,传入树也是不必要的。 E.g:
protected function generateCategoryMenu($categories) {
$tree = "<ul>"; // initialize fresh
foreach($categories as $category) {
$tree .= "<li>" . $category->getName();
if($category->getChildren() != null) {
// just add the result
$tree .= $this->generateCategoryMenu($category->getChildren());
}
$tree .= "</li>";
}
$tree .= "</ul>";
return $tree;
}