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 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计