1

I created a comment form manually, where the user can comment about a certain event:

<form action="{{ path('mql14mqlme_oneeventdisplay', { 'id': app.session.get('idevent') }) }}" method="post" id="commentform">
                        <h3 class="heading">LEAVE A REPLY</h3>


                        <textarea name="comment" id="comment"  tabindex="4"></textarea>

                        <p><input name="submit" type="submit" id="submit" tabindex="5" value="Post" /></p>


                    </form>

The route for the action from routing.yml:

mql14mqlme_oneeventdisplay:
    pattern: /evenement/display/{id}
    defaults: { _controller: Mql14mqlmeBundle:Event:display}

And the action from the controller:

public function displayAction($id,Request $request)
{
    $em = $this->container->get('doctrine')->getEntityManager();
    $evenements = $em->find('Mql14\mqlmeBundle\Entity\Evenement', $id);

$categories= $em->getRepository('Mql14mqlmeBundle:Categorie')->findAll();    
    $query1 = $this->container->get('doctrine')->getEntityManager()->createQuery( 'SELECT c FROM Mql14mqlmeBundle:Commentaire c' . ' JOIN c.evenement e  '  . ' WHERE e.id=:id' . ' ')
    ->setParameter('id', $id);
    $com=$query1->getResult();
     $ca=$evenements->getCategorie();

     $iduser = $this->get('session')->get('iduser');
     $user = $em->getRepository('Mql14mqlmeBundle:User')->find($iduser);

     //commentaire
     $session = $this->getRequest()->getSession();
     $session->set('idevent', $id);
     $idevent = $session->get('idevent');

     if ('POST' === $request->getMethod()) {
    $comment = $request->get('comment');

        $commentaire = new Commentaire();
        $commentaire->setContenu( $comment);
        $commentaire->setUser( $user);
        $commentaire->setEvenement( $evenements);

        $em->persist($commentaire);


     }

    return $this->render('Mql14mqlmeBundle:Default:singleevent.html.twig', array(
            'evenements' => $evenements,
            'categories' => $categories,
            'com'=>$com,
            'ca'=>$ca,
            'user'=>$user,

    ));
} 

And the entity that I want to persist in the database Commentaire.php:

    <?php

    namespace Mql14\mqlmeBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;

    /**
     * Mql14\mqlmeBundle\Entity\Commentaire
     *
     * @ORM\Table(name="commentaire")
     * @ORM\Entity
     */
    class Commentaire
    {
        /**
         * @var integer $id
         *
         * @ORM\Column(name="id", type="integer", nullable=false)
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="IDENTITY")
         */
        private $id;

        /**
         * @var string $contenu
         *
         * @ORM\Column(name="contenu", type="string", length=250, nullable=true)
         */
        private $contenu;

        /**
         * @var User
         *
         * @ORM\ManyToOne(targetEntity="User")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="User_id", referencedColumnName="id")
         * })
         */
        private $user;

        /**
         * @var Evenement
         *
         * @ORM\ManyToOne(targetEntity="Evenement")
         * @ORM\JoinColumns({
         *   @ORM\JoinColumn(name="Evenement_id", referencedColumnName="id")
         * })
         */
        private $evenement;



        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }

        /**
         * Set contenu
         *
         * @param string $contenu
         */
        public function setContenu($contenu)
        {
            $this->contenu = $contenu;
        }

        /**
         * Get contenu
         *
         * @return string 
         */
        public function getContenu()
        {
            return $this->contenu;
        }

        /**
         * Set user
         *
         * @param Mql14\mqlmeBundle\Entity\User $user
         */
        public function setUser(\Mql14\mqlmeBundle\Entity\User $user)
        {
            $this->user = $user;
        }

        /**
         * Get user
         *
         * @return Mql14\mqlmeBundle\Entity\User 
         */
        public function getUser()
        {
            return $this->user;
        }

        /**
         * Set evenement
         *
         * @param Mql14\mqlmeBundle\Entity\Evenement $evenement
         */
        public function setEvenement(\Mql14\mqlmeBundle\Entity\Evenement $evenement)
        {
            $this->evenement = $evenement;
        }

        /**
         * Get evenement
         *
         * @return Mql14\mqlmeBundle\Entity\Evenement 
         */
        public function getEvenement()
        {
            return $this->evenement;
        }
}

I'm not getting any errors, but the comment is not inserted into the database. (in the controller idevent and iduser are two session variables that I work with in the entire project) Thank you

1 Answer 1

2

Please check that you do perist and then flash.

$entityManager->persist($entity);
$entityManager->flush();
Sign up to request clarification or add additional context in comments.

1 Comment

It's working, all I nedded to do is add the last line, the flush command. Thank you SpiRi7.

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.