gpt4 book ai didi

forms - Symfony Doctrine - 如何生成 optgroup 选择表单

转载 作者:行者123 更新时间:2023-12-04 16:44:29 29 4
gpt4 key购买 nike

在 symfony2 中,我想生成多选。
我想得到这样的东西:

 <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 看起来像:
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

任何人都可以帮助我吗?

最佳答案

感谢 a.aitboudad 和我的一个 friend ,我找到了解决方案。

我必须输入我的 Locaton 实体:

    public function getParentName() { 
return $this->getParent() ? $this->getParent()->getName() : null;
}

然后我通过以下方式生成了我的表格:
$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;
}
))

关于forms - Symfony Doctrine - 如何生成 optgroup 选择表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21798180/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com