2014-08-28 36 views
0

我需要在窗體中爲我的類別選項列表創建一個樹結構。 的選項列表我的表單類型代碼:下拉選項結構

->add('discipline', 'entity', array('label' => 'Parent Discipline', 
    'empty_value' => 'Parent Discipline...', 
    'required'  => true, 
    'empty_data' => null, 
    'class'   => 'RFQ\IronilBundle\Entity\ProductionType', 
    'query_builder' => function(ProductionTypeRFQRepository $er) {return $er->createQueryBuilder('w')->where('w.parent IS NULL')->addOrderBy('w.name', 'ASC');}, 
    'attr'   => array('class'=>'form-control login-input'))) 

正如你可以看到我已經連接的存儲庫,這將在樹形結構數據庫中獲取選項列表,但我不知道究竟是如何做到這一點。現在我的Repository如下所示:

<?php 

namespace RFQ\IronilBundle\Entity; 

use Doctrine\ORM\EntityRepository; 

class ProductionTypeRFQRepository extends EntityRepository 
{ 
    public function findAllParents() 
    { 
     return $this->getEntityManager() 
      ->createQuery('SELECT p FROM RFQIronilBundle:ProductionType p WHERE p.parent IS NULL ORDER BY p.name ASC') 
      ->getResult(); 
    } 

    public function findAll() 
    { 
     return $this->findBy(array(), array('name' => 'ASC')); 
    } 
} 

此存儲庫僅獲取父項,但不是來自數據庫的子項。

請給出一些信息在哪裏可以找到解決方案。

編輯:

對於請求,這是我類別實體:

<?php 

namespace RFQ\IronilBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* ProductionType 
* 
* @ORM\Table(name="production_type") 
* @ORM\Entity(repositoryClass="RFQ\IronilBundle\Entity\ProductionTypeRepository") 
* @ORM\Entity(repositoryClass="RFQ\IronilBundle\Entity\ProductionTypeRFQRepository") 
*/ 
class ProductionType 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\Column(type="string") 
    * @ORM\OneToMany(targetEntity="RFQ", mappedBy="discipline") 
    */ 
    protected $name; 

    /** 
    * @ORM\OneToMany(targetEntity="ProductionType", mappedBy="parent") 
    * @ORM\OrderBy({"name" = "ASC"}) 
    **/ 
    protected $children; 

    /** 
    * @ORM\ManyToOne(targetEntity="ProductionType", inversedBy="children") 
    **/ 
    protected $parent; 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 
    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $this->children = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    /** 
    * Set name 
    * 
    * @param string $name 
    * @return ProductionType 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Add children 
    * 
    * @param \RFQ\IronilBundle\Entity\ProductionType $children 
    * @return ProductionType 
    */ 
    public function addChild(\RFQ\IronilBundle\Entity\ProductionType $children) 
    { 
     $this->children[] = $children; 

     return $this; 
    } 

    /** 
    * Remove children 
    * 
    * @param \RFQ\IronilBundle\Entity\ProductionType $children 
    */ 
    public function removeChild(\RFQ\IronilBundle\Entity\ProductionType $children) 
    { 
     $this->children->removeElement($children); 
    } 

    /** 
    * Get children 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getChildren() 
    { 
     return $this->children; 
    } 

    /** 
    * Set parent 
    * 
    * @param \RFQ\IronilBundle\Entity\ProductionType $parent 
    * @return ProductionType 
    */ 
    public function setParent(\RFQ\IronilBundle\Entity\ProductionType $parent = null) 
    { 
     $this->parent = $parent; 

     return $this; 
    } 

    /** 
    * Get parent 
    * 
    * @return \RFQ\IronilBundle\Entity\ProductionType 
    */ 
    public function getParent() 
    { 
     return $this->parent; 
    } 

    public function __toString() 
    { 
     return $this->name; 
    } 
} 

而且實體爲我的RFQ中,我需要用類此樹結構(我已刪除了所有項目這doens't需要問題):

<?php 

namespace RFQ\IronilBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* RFQ 
* 
* @ORM\Table(name="rfq") 
* @ORM\Entity 
*/ 
class RFQ 
{ 
    //RFQ overall 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\ManyToOne(targetEntity="ProductionType",inversedBy="name") 
    * @ORM\JoinColumn(referencedColumnName="id") 
    **/ 
    protected $discipline; 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set discipline 
    * 
    * @param \RFQ\IronilBundle\Entity\ProductionType $discipline 
    * @return RFQ 
    */ 
    public function setDiscipline(\RFQ\IronilBundle\Entity\ProductionType $discipline = null) 
    { 
     $this->discipline = $discipline; 

     return $this; 
    } 

    /** 
    * Get discipline 
    * 
    * @return \RFQ\IronilBundle\Entity\ProductionType 
    */ 
    public function getDiscipline() 
    { 
     return $this->discipline; 
    } 
} 
+0

你能粘貼實體嗎? – 2014-08-28 09:04:54

回答

0

隨着一些升的咖啡和能量飲料我管理如何解決我的p roblem。我會在這裏寫一些類似教程的文章來幫助每個遇到這個問題的人,因爲很難找到一個很好的教程來解決這個問題。

  1. 這些軟件包添加到您的composen.json和運行composer update

    "yavin/symfony-form-tree": "dev-master", 
    "gedmo/doctrine-extensions": "dev-master", 
    "stof/doctrine-extensions-bundle": "1.1.*@dev" 
    

    捆綁鏈接:symfony-form-tree; DoctrineExtensions; StofDoctrineExtensionsBundle

  2. 加入束後,您需要在AppKernel.php

    new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle()

  3. 註冊StofDoctrineExtensionBundle現在,你需要正確的Gedmo添加註解類實體(我的類別調用ProductionType.php

    <?php 
    
    namespace RFQ\IronilBundle\Entity; 
    
    use Doctrine\ORM\Mapping as ORM; 
    use Gedmo\Mapping\Annotation as Gedmo; 
    
    /** 
    * @ORM\Entity 
    * @ORM\Table(name="production_type") 
    * @Gedmo\Tree(type="nested") 
    * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository") 
    */ 
    class ProductionType 
    { 
        /** 
        * @var integer 
        * 
        * @ORM\Column(name="id", type="integer") 
        * @ORM\Id 
        * @ORM\GeneratedValue(strategy="AUTO") 
        */ 
        private $id; 
    
        /** 
        * @ORM\Column(type="string") 
        * @ORM\OneToMany(targetEntity="RFQ", mappedBy="discipline") 
        */ 
        protected $name; 
    
        /** 
        * @ORM\OneToMany(targetEntity="ProductionType", mappedBy="parent") 
        * @ORM\OrderBy({"name" = "ASC"}) 
        **/ 
        protected $children; 
    
        /** 
        * @ORM\ManyToOne(targetEntity="ProductionType", inversedBy="children") 
        * @Gedmo\TreeParent 
        **/ 
        protected $parent; 
    
        /** 
        * @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; 
        /** 
        * @var integer 
        * 
        * @Gedmo\TreeRoot 
        * @ORM\Column(type="integer", nullable=true) 
        */ 
        private $treeRoot; 
    
        // Generated setters and getters 
    
  4. 爲你的映射添加擴展並激活你需要的擴展(在我的例子中爲Tree)

    stof_doctrine_extensions: 
        orm: 
         default: 
          tree: true 
    
  5. 現在您需要與symfony-form-tree一起工作。首先Resources/config/services.xml

    <service id="symfony.form.type.tree" class="Yavin\Symfony\Form\Type\TreeType"> 
        <argument type="service" id="property_accessor"/> 
        <tag name="form.type" alias="y_tree"/> 
    </service> 
    
    <service id="symfony.form.type_guesser.tree" class="Yavin\Symfony\Form\Type\TreeTypeGuesser"> 
        <argument type="service" id="doctrine"/> 
        <tag name="form.type_guesser"/> 
    </service> 
    
  6. 添加服務最後,你需要爲symfony-form-tree,以創建分層下拉選項列表創建表單生成器:

    ->add('discipline', 'y_tree', array(
        'class' => 'RFQ\IronilBundle\Entity\ProductionType', // tree class 
        'levelPrefix' => '--', 
        'orderFields' => array('treeRoot', 'treeLeft'), 
        'prefixAttributeName' => 'data-level-prefix', 
        'treeLevelField' => 'treeLevel', 
    )) 
    

這一切!我並不是說這是製作分層下拉選項列表的正確方法,也許我犯了一些錯誤,但這對我來說是完美的。