大家好我有一个小问题我想获取所有根类别和子类别并将其列在树枝中。
我的数据库表如下所示:
+----+----------+------------------+--------+
| ID | Parent | Name | Status |
+----+----- ----+------------------+--------+
| 1 | | Electronics | 1 |
| 2 | 1 | Camcorders | 1 |
| 3 | 1 | Computers | 1 |
| 4 | 1 | TV Audio | 1 |
| 5 | | Motors | 1 |
| 6 | 5 | Cars & Trucks | 1 |
| 7 | 5 | Motorcycles | 1 |
| 8 | 5 | Boats | 1 |
+----+----------+------------------+--------+
根类别没有父级且具有空值,所有子类别都包含根类别的ID。
树看起来像这样:
|—Electronics
|— Camcorders
|— Computers
|— TV Audio
|—Motors
|— Cars & Trucks
|— Motorcycles
|— Boats
注意:我不需要嵌套的设置数据(树数据)这个树只是示例
我想在这个下拉菜单中显示所有内容:
+---------------------+--------+
| Electroics | Motors |
+---------------------+--------+
| Camcorders |
+------------+
| Computers |
+------------+
| TV Audio |
+------------+
我获取所有根类别的存储库方法:
public function getAllCategoriesByName()
{
return $this->getEntityManager()
->createQuery(
'SELECT c FROM ISLabECommerceBundle:Category c WHERE c.parent IS NULL '
)
->getResult();
}
控制器:
$em = $this->container->get('doctrine');
$categories = $em->getRepository('ISLabECommerceBundle:Category')->getAllCategoriesByName();
嫩枝:
<ul>
{% for category in categories %}
<li><a href='#'><span>{{ category.name }}</span></a></li>
{% endfor %}
</ul>
现在我需要使用JOIN来显示类别电子的所有子类别,但我不知道这样做。我在加入,左/右连接方面的表现很低。 Any1可以举例说明如何获取any1根类别的所有根和显示子类别。
更新
实体
<?php
namespace ISLab\Bundle\ECommerceBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Category
*
* @ORM\Table(name="ecomerce_categories")
* @ORM\HasLifecycleCallbacks()
* @ORM\Entity(repositoryClass="ISLab\Bundle\ECommerceBundle\Repository\CategoryRepository")
*/
class Category
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var integer
*
* @ORM\Column(name="image", type="integer")
*/
private $image;
/**
* @var category
*
* @ORM\OneToMany(targetEntity="category", mappedBy="parent")
*/
private $children;
/**
* @var ArrayCollection
*
* @ORM\ManyToOne(targetEntity="category", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $parent;
/**
* @var integer
*
* @ORM\Column(name="status", type="integer")
*/
private $status;
/**
* @var string
*
* @ORM\Column(name="page_title", type="string", length=255)
*/
private $pageTitle;
/**
* @var string
*
* @ORM\Column(name="meta_keywords", type="text")
*/
private $metaKeywords;
/**
* @var string
*
* @ORM\Column(name="meta_description", type="text")
*/
private $metaDescription;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* @var \DateTime
*
* @ORM\Column(name="date_created", type="datetime")
*/
private $dateCreated;
/**
* @var \DateTime
*
* @ORM\Column(name="date_modified", type="datetime")
*/
private $dateModified;
/**
* Constructor
*
* ArrayCollection
*/
public function __construct()
{
$this->children = new ArrayCollection();
}
/**
* To String
*
* @return string
*/
public function __toString()
{
return (string) $this->name;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set image
*
* @param integer $image
* @return Category
*/
public function setImage($image)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return integer
*/
public function getImage()
{
return $this->image;
}
/**
* Set parent
*
* @param integer $parent
* @return Category
*/
public function setParent($parent)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* @return integer
*/
public function getParent()
{
return $this->parent;
}
/**
* Set status
*
* @param integer $status
* @return Category
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* @return integer
*/
public function getStatus()
{
return $this->status;
}
/**
* Set pageTitle
*
* @param string $pageTitle
* @return Category
*/
public function setPageTitle($pageTitle)
{
$this->pageTitle = $pageTitle;
return $this;
}
/**
* Get pageTitle
*
* @return string
*/
public function getPageTitle()
{
return $this->pageTitle;
}
/**
* Set metaKeywords
*
* @param string $metaKeywords
* @return Category
*/
public function setMetaKeywords($metaKeywords)
{
$this->metaKeywords = $metaKeywords;
return $this;
}
/**
* Get metaKeywords
*
* @return string
*/
public function getMetaKeywords()
{
return $this->metaKeywords;
}
/**
* Set metaDescription
*
* @param string $metaDescription
* @return Category
*/
public function setMetaDescription($metaDescription)
{
$this->metaDescription = $metaDescription;
return $this;
}
/**
* Get metaDescription
*
* @return string
*/
public function getMetaDescription()
{
return $this->metaDescription;
}
/**
* Set description
*
* @param string $description
* @return Category
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set dateCreated
*
* @ORM\PrePersist
*
* @return Category
*/
public function setDateCreated()
{
$this->dateCreated = new \DateTime();
return $this;
}
/**
* Get dateCreated
*
* @return \DateTime
*/
public function getDateCreated()
{
return $this->dateCreated;
}
/**
* Set dateModified
*
* @ORM\PrePersist
* @ORM\PreUpdate
*
* @return Category
*/
public function setDateModified()
{
$this->dateModified = new \DateTime();
return $this;
}
/**
* Get dateModified
*
* @return \DateTime
*/
public function getDateModified()
{
return $this->dateModified;
}
}
答案 0 :(得分:2)
首先修改您的实体,如下所示
public function setChildren($children) { //this will be an array collection so please pay attention
$this->children = $children;
return $this;
}
public function getChildren() {
return $this->children;
}
您还应更改变量名称,因为children
将是ArrayCollection
所以我建议在childrens
中更改它(对于代码语义更好)
然后你可以直接在树枝内迭代孩子
<ul>
{% for category in categories %}
<li><a href='#'><span>{{ category.name }}</span></a></li>
<ul>
{% for sub_category in category.children %}
<li><a href='#'><span>{{ sub_category.name }}</span></a></li>
{% endfor %}
</ul>
{% endfor %}
</ul>
Doctrine将为您完成,因为您的实体是&#34;链接&#34;儿童与父母的关系。在twig中调用category.children
时,将调用getChildren()
方法。
全部