如何使用switch case来保存来自许多实体的实体 我使用变量来依赖实体 这段代码可能会帮助你理解我的问题??
public function addAction($name, Request $request) {
switch ($name) {
case 'Article':
$name = new Article();
$nameType = new ArticleType();
break;
case 'Comment':
$name = new Comment();
$nameType = new CommentType();
break;
case 'Blog':
$name = new Blog();
$nameType = new BlogType();
break;
return $name;
return $nameType;
}
$form = $this->get('form.factory')->create($nameType, $name);
$em = $this->getDoctrine()->getManager();
$em->persist($name);
$em->flush();
return $this->render('WFBundle:Blog:add.html.twig', array(
'form' => $form->createView(),
));
}
答案 0 :(得分:2)
Switch Case不是一个函数,在此代码下面使用
public function addAction($name, Request $request) {
switch ($name) {
case 'Article':
$name = new Article();
$nameType = new ArticleType();
break;
case 'Comment':
$name = new Comment();
$nameType = new CommentType();
break;
case 'Blog':
$name = new Blog();
$nameType = new BlogType();
break;
}
$form = $this->get('form.factory')->create($nameType, $name);
$form->handleRequest($request);
if($form->isSubmitted()){
$em = $this->getDoctrine()->getManager();
$em->persist($name);
$em->flush();
}
return $this->render('WFBundle:Blog:add.html.twig', array(
'form' => $form->createView(),
));
}
所以不要使用返回行,只返回控制器返回