Php 构建类别树Symfony3
我正在尝试在Symfony3中构建一个类别树 我有以下资料: 类别实体:Php 构建类别树Symfony3,php,symfony,symfony-3.3,Php,Symfony,Symfony 3.3,我正在尝试在Symfony3中构建一个类别树 我有以下资料: 类别实体: <?php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository") * @ORM\Table(name="category") */ class Category { /*
<?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();
}
}
问题(和解决方案)非常简单。在递归调用中,传入已部分构建的树:
$tree .= $this->generateCategoryMenu($category->getChildren(), $tree);
// ^--------------------------------------------------------------^
然后将结果连接到调用函数中的树中。在这一点上有效地复制了整个树
解决方法:就是不要从树上往下传。这样一来,也就不需要在树上通行了。例如:
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;
}
受保护函数GenerateCategory菜单($categories){
$tree=“”;//初始化刷新
foreach($categories作为$category){
$tree.=“- ”$category->getName();
如果($category->getChildren()!=null){
//只需添加结果
$tree.=$this->generateCategoryMenu($category->getChildren());
}
$tree.=“
”;
}
$tree.=“
”;
返回$tree;
}
问题(和解决方案)非常简单。在递归调用中,传入已部分构建的树:
$tree .= $this->generateCategoryMenu($category->getChildren(), $tree);
// ^--------------------------------------------------------------^
然后将结果连接到调用函数中的树中。在这一点上有效地复制了整个树
解决方法:就是不要从树上往下传。这样一来,也就不需要在树上通行了。例如:
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;
}
受保护函数GenerateCategory菜单($categories){
$tree=“”;//初始化刷新
foreach($categories作为$category){
$tree.=“- ”$category->getName();
如果($category->getChildren()!=null){
//只需添加结果
$tree.=$this->generateCategoryMenu($category->getChildren());
}
$tree.=“
”;
}
$tree.=“
”;
返回$tree;
}
天哪。。。。你是我的英雄!已经为这个问题挣扎了几天了,现在你来了!谢谢你,伙计!哦,我的上帝。。。。你是我的英雄!已经为这个问题挣扎了几天了,现在你来了!谢谢你,伙计!