Reputation: 281
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(),
));
}
Upvotes: 1
Views: 39
Reputation: 1373
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()));
Upvotes: 1