[英]Symfony form entity collection custom object builder
我的應用程序通過主機路由處理多個車間,因此幾乎每個表都有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();
}
通常,我通過PostPersist Event來做到這一點 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.