我的symfony 2项目需要将用户分配到公司层次结构。所以我尝试在这个项目中使用StofDoctrineExtension包树。这是我的“类别”实体类:
<?php
namespace Application\Sonata\UserBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
/**
* @Gedmo\Tree(type="nested")
* @ORM\Table(name="category")
* @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
*/
class Category
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
private $id;
/**
* @ORM\Column(length=64)
*/
private $title;
/**
* @var integer
*
* @Gedmo\TreeLeft
* @ORM\Column(type="integer")
*/
private $treeLeft;
/**
* @var integer
*
* @Gedmo\TreeLevel
* @ORM\Column(type="integer")
*/
private $treeLevel;
/**
* @var integer
*
* @Gedmo\TreeRight
* @ORM\Column(type="integer")
*/
private $treeRight;
/**
* @Gedmo\TreeParent
* @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $parent;
/**
* @Gedmo\TreeRoot
* @ORM\Column(type="integer", nullable=true)
*/
private $root;
/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
*/
private $children;
/**
* @ORM\OneToMany(targetEntity="User", mappedBy="category")
*/
private $users;
public function __toString()
{
return ($this->title) ? $this->title : '单位';
}
/**
* Constructor
*/
public function __construct()
{
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* @param string $title
*
* @return Category
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Get treeLeft
*
* @return integer
*/
public function getTreeLeft()
{
return $this->treeLeft;
}
/**
* Get treeLevel
*
* @return integer
*/
public function getTreeLevel()
{
return $this->treeLevel;
}
/**
* Get treeRight
*
* @return integer
*/
public function getTreeRight()
{
return $this->treeRight;
}
/**
* Get root
*
* @return integer
*/
public function getRoot()
{
return $this->root;
}
/**
* Set parent
*
* @param \Application\Sonata\UserBundle\Entity\Category $parent
*
* @return Category
*/
public function setParent(\Application\Sonata\UserBundle\Entity\Category $parent = null)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* @return \Application\Sonata\UserBundle\Entity\Category
*/
public function getParent()
{
return $this->parent;
}
/**
* Get children
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getChildren()
{
return $this->children;
}
/**
* Add user
*
* @param \Application\Sonata\UserBundle\Entity\User $user
*
* @return Category
*/
public function addUser(\Application\Sonata\UserBundle\Entity\User $user)
{
$this->users[] = $user;
return $this;
}
/**
* Remove user
*
* @param \Application\Sonata\UserBundle\Entity\User $user
*/
public function removeUser(\Application\Sonata\UserBundle\Entity\User $user)
{
$this->users->removeElement($user);
}
/**
* Get users
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
}
和我的CategoryAdmin.php:
......
/**
* {@inheritdoc}
*/
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title')
->add('parent', 'entity', array(
'class' => 'Application\Sonata\UserBundle\Entity\Category', // tree class
'query_builder' => function($er) {
return $er->createQueryBuilder('c')
->orderBy('c.treeLeft', 'ASC');
},
'required' => false,
))
;
}
当我创建一个类别实体时,只正确设置了父值和根值,treeLeft,treeRight和treeLevel为零。我检查了文档中的示例,它确实提到了有关eventlistener的一些内容。我应该为此添加一个eventlestener吗?如何?谢谢你的帮助。
答案 0 :(得分:0)
删除所有记录并创建新记录后,它就可以了。