插入数据库symfony2

时间:2014-06-24 19:05:11

标签: symfony doctrine dql

我手动创建了一个评论表单,用户可以在其中评论某个事件:

<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>

来自routing.yml的行动路线:

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

来自控制器的动作:

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,

    ));
} 

我希望在数据库中保留的实体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;
        }
}

我没有收到任何错误,但评论未插入数据库。 (在控制器中,idevent和iduser是我在整个项目中使用的两个会话变量) 谢谢

1 个答案:

答案 0 :(得分:2)

请检查您是否进行了检查,然后闪烁。

$entityManager->persist($entity);
$entityManager->flush();