php - Symfony. Use form to add rows to doctrine database. -


well need add rows data table in doctrine. of course know how create database , how add data without form. need make symfony made page add rows databasy via created form.

this route form builder

    /**  * @route("/lol")  */ public function newaction(request $request) {     // createformbuilder shortcut "form factory"     // , call "createbuilder()" on      $form = $this->createformbuilder()         ->add('pytanie', textareatype::class)         ->add('odpowiedz1', texttype::class)         ->add('prawidlowa1', checkboxtype::class)         ->add('odpowiedz2', texttype::class)         ->add('prawidlowa2', checkboxtype::class)         ->add('odpowiedz3', texttype::class)         ->add('prawidlowa3', checkboxtype::class)         ->add('odpowiedz4', texttype::class)         ->add('prawidlowa4', checkboxtype::class)         ->getform();      return $this->render('quiz/new.html.twig', array(         'form' => $form->createview(),     )); } 

this in twig file

    {{ form_start(form) }}     {{ form_widget(form) }}  <input type="submit" /> {{ form_end(form) }} 

and of course created entity.

    <?php  namespace appbundle\entity;  use doctrine\orm\mapping orm;  /**  * pytanie  *  * @orm\table(name="pytanie")  * @orm\entity(repositoryclass="appbundle\repository\pytanierepository")  */ class pytanie {     /**      * @var int      *      * @orm\column(name="id", type="integer")      * @orm\id      * @orm\generatedvalue(strategy="auto")      */     private $id;      /**      * @var string      *      * @orm\column(name="pytanie", type="text")      */     private $pytanie;      /**      * @var string      *      * @orm\column(name="odpowiedz1", type="string", length=255)      */     private $odpowiedz1;      /**      * @var bool      *      * @orm\column(name="prawidlowa1", type="boolean", nullable=true, unique=true)      */     private $prawidlowa1;      /**      * @var string      *      * @orm\column(name="pytanie2", type="string", length=255)      */     private $pytanie2;      /**      * @var bool      *      * @orm\column(name="prawidlowa2", type="boolean", nullable=true, unique=true)      */     private $prawidlowa2;      /**      * @var string      *      * @orm\column(name="odpowiedz3", type="string", length=255)      */     private $odpowiedz3;      /**      * @var bool      *      * @orm\column(name="prawidlowa3", type="boolean", nullable=true, unique=true)      */     private $prawidlowa3;      /**      * @var string      *      * @orm\column(name="odpowiedz4", type="string", length=255)      */     private $odpowiedz4;      /**      * @var bool      *      * @orm\column(name="prawidlowa4", type="boolean", nullable=true, unique=true)      */     private $prawidlowa4;       /**      * id      *      * @return int      */     public function getid()     {         return $this->id;     }      /**      * set pytanie      *      * @param string $pytanie      *      * @return pytanie      */     public function setpytanie($pytanie)     {         $this->pytanie = $pytanie;          return $this;     }      /**      * pytanie      *      * @return string      */     public function getpytanie()     {         return $this->pytanie;     }      /**      * set odpowiedz1      *      * @param string $odpowiedz1      *      * @return pytanie      */     public function setodpowiedz1($odpowiedz1)     {         $this->odpowiedz1 = $odpowiedz1;          return $this;     }      /**      * odpowiedz1      *      * @return string      */     public function getodpowiedz1()     {         return $this->odpowiedz1;     }      /**      * set prawidlowa1      *      * @param boolean $prawidlowa1      *      * @return pytanie      */     public function setprawidlowa1($prawidlowa1)     {         $this->prawidlowa1 = $prawidlowa1;          return $this;     }      /**      * prawidlowa1      *      * @return bool      */     public function getprawidlowa1()     {         return $this->prawidlowa1;     }      /**      * set pytanie2      *      * @param string $pytanie2      *      * @return pytanie      */     public function setpytanie2($pytanie2)     {         $this->pytanie2 = $pytanie2;          return $this;     }      /**      * pytanie2      *      * @return string      */     public function getpytanie2()     {         return $this->pytanie2;     }      /**      * set prawidlowa2      *      * @param boolean $prawidlowa2      *      * @return pytanie      */     public function setprawidlowa2($prawidlowa2)     {         $this->prawidlowa2 = $prawidlowa2;          return $this;     }      /**      * prawidlowa2      *      * @return bool      */     public function getprawidlowa2()     {         return $this->prawidlowa2;     }      /**      * set odpowiedz3      *      * @param string $odpowiedz3      *      * @return pytanie      */     public function setodpowiedz3($odpowiedz3)     {         $this->odpowiedz3 = $odpowiedz3;          return $this;     }      /**      * odpowiedz3      *      * @return string      */     public function getodpowiedz3()     {         return $this->odpowiedz3;     }      /**      * set prawidlowa3      *      * @param boolean $prawidlowa3      *      * @return pytanie      */     public function setprawidlowa3($prawidlowa3)     {         $this->prawidlowa3 = $prawidlowa3;          return $this;     }      /**      * prawidlowa3      *      * @return bool      */     public function getprawidlowa3()     {         return $this->prawidlowa3;     }      /**      * set odpowiedz4      *      * @param string $odpowiedz4      *      * @return pytanie      */     public function setodpowiedz4($odpowiedz4)     {         $this->odpowiedz4 = $odpowiedz4;          return $this;     }      /**      * odpowiedz4      *      * @return string      */     public function getodpowiedz4()     {         return $this->odpowiedz4;     }      /**      * set prawidlowa4      *      * @param boolean $prawidlowa4      *      * @return pytanie      */     public function setprawidlowa4($prawidlowa4)     {         $this->prawidlowa4 = $prawidlowa4;          return $this;     }      /**      * prawidlowa4      *      * @return bool      */     public function getprawidlowa4()     {         return $this->prawidlowa4;     } } 

i created far. i'm stuck. , cant find hint make form write data database.

you need handle form submission :

/**  * @route("/lol")  */ public function newaction(request $request) {      // first create new empty object     $pytanie = new pitanie();       // create form object     $form = $this->createformbuilder($pitanie)         ->add('pytanie', textareatype::class)         ->add('odpowiedz1', texttype::class)         ->add('prawidlowa1', checkboxtype::class)         ->add('odpowiedz2', texttype::class)         ->add('prawidlowa2', checkboxtype::class)         ->add('odpowiedz3', texttype::class)         ->add('prawidlowa3', checkboxtype::class)         ->add('odpowiedz4', texttype::class)         ->add('prawidlowa4', checkboxtype::class)         ->getform();     $form->handlerequest($request);      if ($form->issubmitted() && $form->isvalid()) {          // $form->getdata() holds submitted values          // but, original `$pitanie` variable has been updated         $pitanie = $form->getdata();          // ... perform action, such saving task database          // example, if task doctrine entity, save it!          // $em = $this->getdoctrine()->getmanager();          // $em->persist($task);          // $em->flush();           return $this->redirecttoroute('task_success');     }     return $this->render('quiz/new.html.twig', array(         'form' => $form->createview(),     )); } 

check doc more info : http://symfony.com/doc/current/forms.html#handling-form-submissions


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -