1

I have this simple Entity:

<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="CommentTypes")
 */

class CommentTypes
{
        /**
        * @ORM\Column(type="integer")
        * @ORM\Id
        * @ORM\GeneratedValue(strategy="AUTO")
        */
        protected $id;

        /** @ORM\Column(type="string", length=100) */
        protected $name;
}
?>

After I Generate the CRUD and use it to Create a new record I get this error:

    Error: Call to a member function getId() on a non-object
500 Internal Server Error - FatalErrorException 

Error is in this piece of code - in src/AppBundle/Controller/CommentTypesController.php in this line - "return $this->redirectToRoute('admin_commenttypes_show', array('id' => $commenttypes->getId()));"

 * Creates a new CommentTypes entity.
 *
 * @Route("/new", name="admin_commenttypes_new")
 * @Method({"GET", "POST"})
 */
public function newAction(Request $request)
{
    $commentType = new CommentTypes();
    $form = $this->createForm(new CommentTypesType(), $commentType);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($commentType);
        $em->flush();

        return $this->redirectToRoute('admin_commenttypes_show', array('id' => $commenttypes->getId()));
    }

    return $this->render('commenttypes/new.html.twig', array(
        'commentType' => $commentType,
        'form' => $form->createView(),
    ));
}
2
  • Can you please show us also the code you're using to create a new record? Commented Dec 12, 2015 at 20:25
  • I edited the post with code where the error exist. Commented Dec 12, 2015 at 20:34

1 Answer 1

1

I think you misspelled the $commentType variable on line:

return $this->redirectToRoute('admin_commenttypes_show', array('id' => $commenttypes->getId()));

It should be:

return $this->redirectToRoute('admin_commenttypes_show', array('id' => $commentType->getId()));
Sign up to request clarification or add additional context in comments.

8 Comments

Tried, without success. The CRUD is creating successfully the record, but it throw exception.
Yes, it's working now but like this: return $this->redirectToRoute('admin_commenttypes_show', array('id' => $commentType->getId()));
It is like in my answer, no? Or am I missing something?
Yes same like your answer but you suggested - $commenttype, it's $commentType.
Oh, I get it, I was looking for the differences and couldn't find them. Glad I could help find the issue. ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.