-1

how can i use switch case to persist an entity from many entities i use a variable to rely with the entity this code may be help you to understand my question ??

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(),
    ));
}
Chase
  • 9,289
  • 5
  • 51
  • 77
Ziar Adjel
  • 1
  • 1
  • 3
  • Please your question with error you are facing – Gopal Joshi Oct 24 '16 at 04:54
  • What are these `return $name;`; `return $nameType;`? You should not use the same variable `$name` for the variable passed in action and the entity. There is no `default` in your `switch/case`, `$name` and `$nameType` may be not correctly defined. – Al Foиce ѫ Oct 24 '16 at 07:14

1 Answers1

2

Switch Case not a function, used below this code

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(),
));
}

So not use return lines, return only controller return

Deniz Aktürk
  • 362
  • 1
  • 9