1
什麼是設置鍵/值對(從MySQL獲得)的集合作爲控制器內的選擇字段「選擇」的最佳方式? 我想類似的東西:如何在Symfony2中設置選擇字段值?
$form = $this->createForm(new AddNews(), $news);
$newsList = $this->getDoctrine()
->getRepository('BakaMainBundle:News')->getAllNews();
$titlesList = ...($newsList); // some fuction that extract title=>id
// array from news object collection
$form->get('newsList')->setData($titlesList);
其中AddNews()形式如下:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(...)
->add(...)
->add('accept' , 'submit')
->add('newsList', 'choice', array
(
'mapped' => false,
'required' => true
));
}