我在 symfony 项目中使用Doctrine 扩展树以分层方式管理类别。
尽管如此,我还没有找到如何简单地管理 twig 中的添加/编辑部分:我可以使用childrenHierarchy()
方法显示 html 树结构,但我想更进一步,有这样的东西:
Root
- Cat 1 (+)
-- Cat 1-1 (+)
-- Cat 1-2 (+)
- Cat 2 (+)
(+) 将打开一个显示(类别)文本字段的模式,允许在所选父级上分支此元素。
我没有在文档中看到(如果可能的话,我也没有看到)将此方法childrenHierarchy()
与允许额外 html 的选项和 ajax 调用一起使用。
我的实体是基本的:
<?php
namespace CPASimUSante\SimupollBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Claroline\CoreBundle\Entity\User;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Simupoll categories
*
* @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
* @ORM\Table(
* name="cpasimusante__simupoll_category",
* uniqueConstraints={
* @ORM\UniqueConstraint(name="category_unique_name_and_user", columns={"user_id", "name"})
* }
* )
* @DoctrineAssert\UniqueEntity({"name", "user"})
* @Gedmo\Tree(type="nested")
*/
class Category
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* name of the category
* @var string $value
*
* @ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* @Gedmo\TreeLeft
* @ORM\Column(name="lft", type="integer")
*/
private $lft;
/**
* @Gedmo\TreeLevel
* @ORM\Column(name="lvl", type="integer")
*/
private $lvl;
/**
* @Gedmo\TreeRight
* @ORM\Column(name="rgt", type="integer")
*/
private $rgt;
/**
* @Gedmo\TreeRoot
* @ORM\Column(name="root", type="integer", nullable=true)
*/
private $root;
/**
* @Gedmo\TreeParent
* @ORM\ManyToOne(
* targetEntity="CPASimUSante\SimupollBundle\Entity\Category",
* inversedBy="children"
* )
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $parent;
/**
* @ORM\ManyToOne(targetEntity="Claroline\CoreBundle\Entity\User")
*/
private $user;
/**
* @ORM\OneToMany(
* targetEntity="CPASimUSante\SimupollBundle\Entity\Category",
* mappedBy="parent",
* )
* @ORM\OrderBy({"lft" = "ASC"})
*/
protected $children;
/**
* propoerty used in hierarchy display, like selectbox
*/
private $indentedName;
public function __construct()
{
$this->children = new ArrayCollection();
}
/**
* Returns the resource id.
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Sets the resource id.
* Required by the ResourceController when it creates a fictionnal root
*
* @param integer $id
*/
public function setId($id)
{
$this->id = $id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
/**
* Returns the children resource instances.
*
* @return \Doctrine\Common\ArrayCollection|Category[]
*/
public function getChildren()
{
return $this->children;
}
/**
* Returns the parent category.
*
* @return \CPASimUSante\SimupollBundle\Entity\Category
*/
public function getParent()
{
return $this->parent;
}
/**
* Sets the parent category.
*
* @param \CPASimUSante\SimupollBundle\Entity\Category $parent
*/
public function setParent(\CPASimUSante\SimupollBundle\Entity\Category $parent = null)
{
$this->parent = $parent;
}
/**
* Return the lvl value of the resource in the tree.
*
* @return integer
*/
public function getLvl()
{
return $this->lvl;
}
public function getUser()
{
return $this->user;
}
public function setUser(User $user)
{
$this->user = $user;
}
/**
* allows hierachy display
* @return string
*/
public function __toString()
{
return $this->getName();
}
/**
* allows hierachy display
* @return string
*/
public function getIndentedName()
{
return str_repeat("--", $this->lvl) . $this->name;
//return str_repeat($this->getParent()." > ", $this->lvl) . $this->name;
}
}
我的控制器目前也是基本的,这是我认为我可以调整选项的地方:
public function indexAction(Simupoll $simupoll)
{
$em = $this->getDoctrine()->getManager();
$user = $this->container
->get('security.token_storage')
->getToken()->getUser();
$repo = $em->getRepository('CPASimUSanteSimupollBundle:Category');
$options = array(
'decorate' => true,
'rootOpen' => '<ul>',
'rootClose' => '</ul>',
'childOpen' => '<li>',
'childClose' => '</li>'
);
$htmlTree = $repo->childrenHierarchy(
null, /* starting from root nodes */
false, /* true: load all children, false: only direct */
$options
);
return array(
'_resource' => $simupoll,
'tree' => $htmlTree,
);
}
模板是:
{% block section_content %}
<div class="panel panel-default">
<div class="panel-body" id="text_content">
{{ tree |raw }}
</div>
</div>
{% endblock %}
如果有人有建议或线索,请提前感谢您。