javascript - name age and upload file not shown in database -
studentcontroller.php
i uploading image task in symfony database not not show these item program working , image stored in web\uploads\photos.
this studentcontroller.php 1 can solve problem.
<?php namespace appbundle\controller; use appbundle\entity\student; use appbundle\form\formvalidationtype; use symfony\bundle\frameworkbundle\controller\controller; use sensio\bundle\frameworkextrabundle\configuration\route; use symfony\component\httpfoundation\request; use symfony\component\httpfoundation\response; use symfony\component\form\extension\core\type\texttype; use symfony\component\form\extension\core\type\filetype; use symfony\component\form\extension\core\type\submittype; class studentcontroller extends controller { /** * @route("/student/new") */ public function newaction(request $request) { $student = new student(); $form = $this->createformbuilder($student) ->add('name', texttype::class) ->add('age', texttype::class) ->add('photo', filetype::class, array('label' => 'photo (png, jpeg)')) ->add('save', submittype::class, array('label' => 'submit')) ->getform(); $form->handlerequest($request); if ($form->issubmitted() && $form->isvalid()) { $file = $student->getphoto(); $filename = md5(uniqid()).'.'.$file->guessextension(); $file->move($this->getparameter('photos_directory'), $filename); $student->setphoto($filename); return new response("user photo uploaded."); } else { return $this->render('student/new.html.twig', array( 'form' => $form->createview(), )); } } } **student.php**
this entity class file maked 3 field here 1. name 2. age 3. photo mysql not show me update. enter image description here. enter image description here, file uploaded database not updated
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; /** * student * * @orm\table(name="student") * @orm\entity(repositoryclass="appbundle\repository\studentrepository") */ class student { /** * @var int * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @var string * * @orm\column(name="name", type="string", length=255) */ private $name; /** * @var int * * @orm\column(name="age", type="integer") */ private $age; /** * @var string * * @orm\column(name="photo", type="string", length=255) */ private $photo; /** * id * * @return int */ public function getid() { return $this->id; } /** * set name * * @param string $name * * @return student */ public function setname($name) { $this->name = $name; return $this; } /** * name * * @return string */ public function getname() { return $this->name; } /** * set age * * @param integer $age * * @return student */ public function setage($age) { $this->age = $age; return $this; } /** * age * * @return int */ public function getage() { return $this->age; } /** * set photo * * @param string $photo * * @return student */ public function setphoto($photo) { $this->photo = $photo; return $this; } /** * photo * * @return string */ public function getphoto() { return $this->photo; } } **new.html.twig** {% extends 'base.html.twig' %} {% block javascripts %} <script language = "javascript" src = "https://code.jquery.com/jquery-2.2.4.min.js"></script> {% endblock %} {% block stylesheets %} <style> #simpleform { width:600px; border:2px solid grey; padding:14px; } #simpleform label { font-size:12px; float:left; width:300px; text-align:right; display:block; } #simpleform span { font-size:11px; color:grey; width:100px; text-align:right; display:block; } #simpleform input { border:1px solid grey; font-family:verdana; font-size:14px; color:grey; height:24px; width:250px; margin: 0 0 20px 10px; } #simpleform button { clear:both; margin-left:250px; background:grey; color:#ffffff; border:solid 1px #666666; font-size:16px; } </style> {% endblock %} {% block body %} <h3>student form</h3> <div id="simpleform"> {{ form_start(form) }} {{ form_widget(form) }} {{ form_end(form) }} </div> {% endblock %} parameters: photos_directory: '%kernel.root_dir%/../web/uploads/photos'
you need use doctrine entity manager persist , flush object in db.
$entitymanager->persist($yourentity); $entitymanager->flush($yourentity);
i recommand read symfony documentation doctrine , form.
there controller action fixed:
<?php class studentcontroller extends controller { /** * @route("/student/new") */ public function newaction(request $request) { $student = new student(); $form = $this->createformbuilder($student) ->add('name', texttype::class) ->add('age', texttype::class) ->add('photo', filetype::class, array('label' => 'photo (png, jpeg)')) ->add('save', submittype::class, array('label' => 'submit')) ->getform(); $form->handlerequest($request); if ($form->issubmitted() && $form->isvalid()) { $file = $student->getphoto(); $filename = md5(uniqid()).'.'.$file->guessextension(); $file->move($this->getparameter('photos_directory'), $filename); $student->setphoto($filename); $em = $this->getmanager()->getdoctrine(); $em->persist($student); $em->flush(); return new response("user photo uploaded."); } else { return $this->render('student/new.html.twig', array( 'form' => $form->createview(), )); } } }
Comments
Post a Comment