我需要在窗體中爲我的類別選項列表創建一個樹結構。 的選項列表我的表單類型代碼:下拉選項結構
->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;
}
}
你能粘貼實體嗎? – 2014-08-28 09:04:54