dongliang9682 2014-01-03 20:41
浏览 23
已采纳

Symfony2在表单类型中使用doctrine

I would like to create an optgroup in symfony2 like this:

$category_choices = array(
    array('Category 1' => array(
        '1' => 'Option 1...',
        '2' => 'Option 2...',
        '3' => 'Option 3...'
    )),
    array('Category 2' => array(
        '4' => 'Option 4...',
        '5' => 'Option 5...'
    ))
);

$builder->add('category_list', 'choice', array(
    'label' => 'Category',
    'choices' => $category_choices
));

But I want to get the category choices from the database and I need to execute some actions on them and add them to an array. How can I do this in the same form type? (use doctrine in form type)

  • 写回答

1条回答 默认 最新

  • doushang4274 2014-01-03 23:05
    关注

    There are several ways to do that actually.

    Most reasonable seems to be adding additional option to your form, this so question seems to be nice tutorial

    Other than that you can define your form (you should anyway) as a service and pass a doctrine's entity manager into it. This is covered in symfony's tutorials about creating forms and services.

    The least reasonable option is passing doctrine or your choices in constructor manually when creating Form object, note that this is totally non-standard and while it's easiest it's also least extensible.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器