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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题