2016-05-08 10 views
0

symfony 2プロジェクトでは、ユーザを会社の階層に割り当てる必要があります。だから私はこのプロジェクトのStofDoctrineExtensionバンドルのツリーを使用しようとします。treeLeft、treeRight、treeLevelの値がsymfony 2ツリーで更新されていません。

<?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:ここに私のエンティティクラスは、「カテゴリ」のためである私は、カテゴリエンティティ、親のみとルート値が正しく設定されている、treeLeft、treeRightを作成し、

...... 
    /** 
    * {@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, 
      )) 
     ; 
    } 

treeLevelはゼロです。ドキュメントのサンプルをチェックして、イベントリスナーについての説明をしました。これに対してイベントリスナーを追加する必要がありますか?そしてどうやって?助けてくれてありがとう。

+0

私はちょうどフロントエンドコントローラにfolowingコードを使用しました: '$ food = new Category(); $ food-> setTitle( 'Food'); $ fruits = newカテゴリ(); $ fruits-> setTitle( 'Fruits'); $ fruits-> setParent($ food); $ vegetables =新しいカテゴリ(); $ vegetables-> setTitle( 'Vegetables'); $ vegetables-> setParent($ food); $ carrots = newカテゴリ(); $ carrots-> setTitle( 'Carrots'); $ carrots-> setParent($ vegetables); $ this-> em-> persist($ food); $ this-> em-> persist($ fruits); $ this-> em-> persist($ vegetables); $ this-> em-> persist($ carrots); $ this-> em-> flush();これは機能しています。だからそれはsonataadminに関連しているようだが、ツリーには関係していないようだ誰でも? – fallcool

答えて

0

すべてのレコードを削除して新しいレコードを作成した後で動作します。

関連する問題