php - An exception occurred while executing 'INSERT INTO events (user_id, ET1, ET2) VALUES (?, ?, ?)' with params [null, 2, 3]: -


i trying create form inside default controller. want values both dropdowns in homepage stored in et1 , et2 columns of events table, able do. want user_id logged in user should stored in user_id column of events table. while trying gives me error:

an exception occurred while executing 'insert events (user_id, et1, et2) values (?, ?, ?)' params [null, 2, 3]: sqlstate[23000]: integrity constraint violation: 1048 column 'user_id' cannot null

here code defaultcontroller

<?php  namespace appbundle\controller;  use appbundle\entity\events; //use appbundle\entity\eventtype; use appbundle\entity\users; use sensio\bundle\frameworkextrabundle\configuration\route; use symfony\bundle\frameworkbundle\controller\controller; use symfony\component\httpfoundation\request; use symfony\component\httpfoundation\response; use symfony\component\form\extension\core\type\submittype; use symfony\component\form\extension\core\type\choicetype;  class defaultcontroller extends controller {      /**      * @route("/home", name="homepage")      */     public function indexaction(request $request) {         $events = new events();          $form = $this->createformbuilder($events)                 ->add('et1', choicetype::class, array(                     'choices' => array(                         'poker' => 1,                         'chess' => 2,                         'cricket' => 3,                         'marbles' => 4,                         'football' => 5,                     ),                     'choices_as_values' => true,                 ))                 ->add('et2', choicetype::class, array(                     'choices' => array(                         'poker' => 1,                         'chess' => 2,                         'cricket' => 3,                         'marbles' => 4,                         'football' => 5,                     ),                     'choices_as_values' => true,                 ))                 ->add('save', submittype::class, array('label' => 'submit'))                 ->getform();          if ($request->ismethod('post')) {             $form->submit($request);              if ($form->isvalid()) {                 // perform action, eg. persisting data database...                 $user = $this->container->get('security.context')->gettoken()->getuser();                 $id = $user->getid(); //               var_dump($id); //                exit;                 $events->setuserid($id);                   $em = $this->getdoctrine()->getmanager();                  // tells doctrine want (eventually) save product (no queries yet)                 $em->persist($events);                  // executes queries (i.e. insert query)                 $em->flush();                 return $this->redirecttoroute('homepage');             }         }          return $this->render('default/index.html.twig', array(                     'form' => $form->createview(),         ));     } 

below users etity

<?php  namespace appbundle\entity;  use fos\userbundle\model\user baseuser; use doctrine\orm\mapping orm; use symfony\component\validator\constraints assert; use doctrine\common\collections\arraycollection;  /**  * users  *  * @orm\table(name="users")  * @orm\entity(repositoryclass="appbundle\repository\usersrepository")  */ class users extends baseuser {     /**      * @orm\column(name="id", type="integer")      * @orm\id      * @orm\generatedvalue(strategy="auto")      */     protected $id;          /**      * id      * @return integer       */     public function getid()     {         return $this->id;     }      /**      * @orm\onetomany(targetentity="events", mappedby="users")      */     protected $multievents;       public function __construct()     {         parent::__construct();         $this->multievents = new arraycollection();             }      } 

below events entity

<?php  namespace appbundle\entity;  use doctrine\orm\mapping orm;  /**  * events  *  * @orm\table(name="events")  * @orm\entity(repositoryclass="appbundle\repository\eventsrepository")  */ class events {     /**      * @var int      *      * @orm\column(name="id", type="integer")      * @orm\id      * @orm\generatedvalue(strategy="auto")      */     protected $id;      /**      * @var int      *      * @orm\column(name="user_id", type="integer")      */     protected $user_id;      /**      * @var int      *      * @orm\column(name="et1", type="integer")      */     protected $et1;      /**      * @var int      *      * @orm\column(name="et2", type="integer")      */     protected $et2;      /**      * @orm\manytoone(targetentity="users", inversedby="multievents")      * @orm\joincolumn(name="user_id", referencedcolumnname="id")      */     private $singleuser;       /**      * id      *      * @return integer       */     public function getid()     {         return $this->id;     }      /**      * user_id      *      * @return integer       */     public function getuser_id()     {         return $this->user_id;     }      /**      * set user_id      *      * @param integer $user_id      * @return events      */     public function setuserid($user_id)     {         $this->user_id = $user_id;          return $this;     }      /**      * set et1      *      * @param integer $et1      * @return events      */     public function setet1($et1)     {         $this->et1 = $et1;          return $this;     }      /**      * et1      *      * @return integer       */     public function getet1()     {         return $this->et1;     }      /**      * set et2      *      * @param integer $et2      * @return events      */     public function setet2($et2)     {         $this->et2 = $et2;          return $this;     }      /**      * et2      *      * @return integer       */     public function getet2()     {         return $this->et2;     }  } 

instead of setting user_id need set user object on events.

so expose setter on singleuser on events class (you can remove definition of user_id property defined relation)

events

public function setuser($user) {  this->singleuser = $user; } 

and set directly user object in controller:

if ($form->isvalid()) {         $user = $this->container->get('security.context')->gettoken()->getuser();         $events->setuser($user);   ... 

hope help


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -