2014-08-28 3 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'))) 

당신은 내가 트리 구조의 데이터베이스에서 옵션 목록을 얻을 것이다 저장소를 연결했지만, 나는 그 작업을 수행하는 방법을 정확히 모르는 볼 수 있듯이. 이제 내 저장소는 다음과 같습니다.

<?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')); 
    } 
} 

이 저장소는 부모 만 가져 오지만 데이터베이스의 하위는 가져 오지 않습니다.

여기에 대한 해결책을 찾을 수있는 정보를 제공해주십시오.

편집 : 요청에 대한

,이 범주에 대한 내 엔티티이다 : 나는 (나는 모든 항목을 삭제 한 범주이 트리 구조를 필요로하는 내 RFQ에 대한

<?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; 
    } 
} 

그리고 실체가있는 문제를 해결할 필요가 없음) :

+0

엔티티를 붙여 넣을 수 있습니까? –

답변

0

약간의 커피와 에너지 드링크로 나는 내 p를 해결하는 방법을 관리했습니다. 흠집. 이 튜토리얼과 비슷한 것을 써서이 문제를 가진 모든 사람들을 도울 것입니다. 좋은 튜토리얼을 찾는 방법이 매우 어렵 기 때문에이 문제를 가진 모든 사람들을 도울 것입니다.

  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. 은 번들을 추가 한 후에 당신은 이제 (내 카테고리 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 
    
  3. 카테고리 엔티티 바로 Gedmo 주석을 추가 할 필요가 new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle()

  4. AppKernel.php

    에 StofDoctrineExtensionBundle을 등록해야

    매핑에 확장 기능을 추가하고 필요한 확장 기능을 활성화하십시오 (내 경우 Tree)

    stof_doctrine_extensions: 
        orm: 
         default: 
          tree: true 
    
  5. 이제 symfony-form-tree으로 작업해야합니다. 먼저 계층 드롭 다운 옵션 목록을 만드는 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. 서비스를 추가 : 모든

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

의 그!나는 이것이 계층 적 드롭 다운 옵션 목록을 만드는 올바른 방법이고 어쩌면 내가 몇 가지 실수를했다고 말할 수는 없지만 이것은 완벽하게 작동하고있다.