首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >下拉期权结构

下拉期权结构
EN

Stack Overflow用户
提问于 2014-08-28 08:57:09
回答 1查看 343关注 0票数 0

我需要在表单中为我的类别选项列表创建一个树结构。我的表单输入“选项”列表代码:

代码语言:javascript
复制
->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')))

正如您所看到的,我已经建立了连接存储库,它将从树结构中的数据库中获取选项列表,但我不知道如何做到这一点。现在我的仓库看起来是这样的:

代码语言:javascript
复制
    <?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'));
    }
}

这个存储库只获取父库,而不是数据库中的子库。

请提供一些信息,在哪里找到解决这一问题的办法。

编辑:

对于请求,这是我的实体类别:

代码语言:javascript
复制
    <?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中,我需要这个带有类别的树结构(我已经删除了所有不需要解决问题的项目):

代码语言:javascript
复制
    <?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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-30 15:49:19

用几升咖啡和能量饮料,我设法解决了我的问题。我将在这里写一些类似于教程的文章来帮助每个遇到这个问题的人,因为很难找到一个很好的教程来解决这个问题。

  1. 将这些包添加到composen.json并运行composer update。 "yavin/symfony-form-tree":"dev-master",“gedmo/主义-扩展”:"dev-master",“stof/主义-扩展-包”:"1.1.*@dev“ 到包的链接:符号形树DoctrineExtensionsStofDoctrineExtensionsBundle
  2. 添加包后,需要在StofDoctrineExtensionBundle中注册AppKernel.php new Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle()
  3. 现在您需要向类别实体添加正确的Gedmo注释(我的类别调用ProductionType.php)
  4. 向映射添加扩展并激活所需的扩展(在我的例子中是树) stof_doctrine_extensions: orm:默认值: tree: true
  5. 现在,您需要使用symfony-form-tree。首先向Resources/config/services.xml添加服务
  6. 最后,您需要为symfony-form-tree创建表单生成器以创建分层下拉选项列表: ->add(“纪律”,“y_ tree”,数组( ' class‘=> 'RFQ\IronilBundle\Entity\ProductionType',//树类'levelPrefix’=>‘-’,'orderFields‘=>数组(’treeRoot‘,'treeLeft'),'prefixAttributeName’=>‘数据级-前缀,'treeLevelField’=>‘树级’,)

就这样!我并不是说这是制作分层下拉选项列表的正确方法,也许我犯了一些错误,但这对我来说是完美的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25544715

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档