在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
有人可以帮我解决这个问题吗?