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(),
    ));
}
Alexandru Furculita
  • 1,374
  • 9
  • 19
eXtreme
  • 244
  • 3
  • 18

1 Answers1

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()));
Alexandru Furculita
  • 1,374
  • 9
  • 19
  • Tried, without success. The CRUD is creating successfully the record, but it throw exception. – eXtreme Dec 12 '15 at 20:44
  • Yes, it's working now but like this: `return $this->redirectToRoute('admin_commenttypes_show', array('id' => $commentType->getId()));` – eXtreme Dec 12 '15 at 20:50
  • It is like in my answer, no? Or am I missing something? – Alexandru Furculita Dec 12 '15 at 20:52
  • Yes same like your answer but you suggested - $commenttype, it's $commentType. – eXtreme Dec 12 '15 at 20:53
  • Oh, I get it, I was looking for the differences and couldn't find them. Glad I could help find the issue. ;) – Alexandru Furculita Dec 12 '15 at 20:54
  • The file is generated with `php console doctrine:generate:entities AppBundle`, how can I make the changes persistent if I change the Entity and generate it again? – eXtreme Dec 12 '15 at 21:02
  • `php console doctrine:generate:entities AppBundle` is only for generating the setters and getters for entity properties. So this will affect only the entity class. Let me know if I answered your question, I'm not sure if I understood it :) – Alexandru Furculita Dec 12 '15 at 21:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97735/discussion-between-extreme-and-alexandru-furculita). – eXtreme Dec 13 '15 at 00:35