【发布时间】:2017-08-27 00:42:46
【问题描述】:
[设置]
- Symfony 3
- BoxEntity:[id,名称]
- CandyEntity:[id, name]
[问题]
目前,在创建新糖果时,我必须选择一个盒子作为父实体。
问题是,我希望这个选择是自动化的。
盒子已经在数据库中注册,会话持有当前盒子参数以便轻松找回。
但是一旦数据发布,我不知道如何将其应用于糖果实体。
[文件]
AppBundle/Controller/CandyController.php
public function newAction(Request $request) {
$$candy= new Candy();
$form = $this->createForm('AppBundle\Form\CandyType', $conference);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($candy);
$em->flush();
return $this->redirectToRoute('candy_show', array('id' => $candy->getId()));
}
return $this->render('candy/new.html.twig', array(
'candy' => $candy,
'form' => $form->createView(),
));
}
AppBundle/Form/CandyType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('nom')
->add('box'); //Remove from form, and set manually
}
我做了read this page,但不知道如何正确操作。
如果有人好心给我一个完整的例子来解决我的问题,将不胜感激。
【问题讨论】:
-
您是将它连接到数据库中的现有框,还是创建一个新框,但已经在会话中设置了参数?会话本身存储了什么?
-
@JasonRoman 该框已存在于数据库中。会话保存盒子数据(id、name)。编辑了我的帖子以添加该详细信息... ;)
标签: php symfony symfony-forms