Symfony主义-如何生成optgroup选择表单 [英] Symfony Doctrine - how to generate optgroup select form
本文介绍了Symfony主义-如何生成optgroup选择表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在symfony2中,我想生成多选选择。
我想得到这样的东西:
In symfony2 I would like to generate multiselect selections. I would like to get something like this :
<select>
<optgroup label="district 1">
<option>city 1</option>
<option>city 2</option>
</optgroup>
<optgroup label="district 2">
<option>city X</option>
<option>city Y</option>
</optgroup>
</select>
我的位置实体为:
class Location
{
/**
* @var integer
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Location", inversedBy="children")
* @ORM\JoinColumn(name="pid", nullable=true)
*/
protected $parent;
/**
* @ORM\OneToMany(targetEntity="Location", mappedBy="parent")
*/
protected $children;
/**
* @var string
* @ORM\Column(name="name", type="string", length=255)
*/
protected $name;
所以mysql看起来像:
so mysql looks like :
id, pid, name
1, null, district 1
2, null, district 2
3, 1, city 1
4, 1, city 2
5, 2, city X
6, 2, city Y
有人可以帮我吗?
推荐答案
thx给a.aitboudad和我的一个朋友,我找到了解决方案。
thx to a.aitboudad and a friend of mine I have found the solution.
我必须输入Locaton实体:
I had to type into my Locaton entity :
public function getParentName() {
return $this->getParent() ? $this->getParent()->getName() : null;
}
然后我通过以下方式生成表单:
then I generated my form by :
$builder->add('locations', 'entity', array(
'class' => 'MyBundle:Location',
'group_by' => 'parentName',
'property' => 'name',
'query_builder' => function (\Doctrine\ORM\EntityRepository $repo) {
$qb = $repo->createQueryBuilder('l');
$qb->andWhere('l.parent IS NOT NULL');
return $qb;
}
))
这篇关于Symfony主义-如何生成optgroup选择表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文