【问题标题】:Symfony form entity collection custom object builderSymfony 表单实体集合自定义对象构建器
【发布时间】:2014-01-25 20:37:19
【问题描述】:

我的应用程序通过主机路由处理多个商店,因此几乎每个表都有shop_id。现在,我有类别和单独的表单来通过CategoriesType 管理它们。

$builder->add('categories', 'bootstrap_collection', array(
        'type' => $this->categoryType,
        'allow_add' => true,
        'allow_delete' => true,
        'error_bubbling' => false,
    ));

categoryType 是一个Type,它由当前商店构造,data_class 设置为 Category 实体。但是需要 shop_it 正确存储在数据库中。

问题是,如何在表单验证之前嵌入逻辑来更改对象 - 如何在新实体上使用 setShop()。

这是我的使用方法:

$categoriesForm = $this->createForm(new MenuCategoriesType(
    $this->get('form.category_type')
), $catering = $this->getUser()->getCatering());
$categoriesForm->handleRequest($request);

if ($categoriesForm->isValid()) {
    $em = $this->getDoctrine()->getManager();
    $em->persist($catering);
    $em->flush();
}

【问题讨论】:

    标签: php forms events symfony


    【解决方案1】:

    通常我用PostPersist 事件来做。

    【讨论】:

    • 其中一个选项,但我使用了级联原则注释以及孤儿删除设置为 true
    猜你喜欢
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    相关资源
    最近更新 更多