首先是Form,第二个是表单详细信息我标记了我面临的错误:
无法在Symfony中上传文件,我的数据库接受“C:\ xampp \ tmp \ php14FE.tmp”文件。我使用正确的方法吗?
**HomeController.php**
<?php
namespace MyBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use MyBundle\Entity\school;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use MyBundle\Models\Login;
//use MyBundle\Entity\Article;
use MyBundle\Entity\User;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Console\Helper\Table;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class HomeController extends Controller
{
/**
* @Route("/", name="form")
*/
public function formAction(Request $request)
{
// create a task and give it some dummy data for this example
$user = new school();
$form = $this->createFormBuilder($user)
->add('userName', TextType::class)
->add('firstName', TextType::class)
->add('lastName', TextType::class)
->add('emailId', EmailType::class)
->add('password', PasswordType::class)
->add('uploadfile', FileType::class)
//->add('form', 'submit', SubmitType::class, array('label' => 'Submit'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
// Get Data
$un = $form['userName']->getData();
$fn = $form['firstName']->getData();
$ln = $form['lastName']->getData();
$email = $form['emailId']->getData();
$pass = $form['password']->getData();
$fil = $form['uploadfile']->getData();
$user->setUserName($un);
$user->setFirstName($fn);
$user->setLastName($ln);
$user->setEmailId($email);
$user->setPassword($pass);
$user->setUploadFile($fil);
$em=$this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
/* $response = new JsonResponse(array('status' => 'success', 'message' =>'Data has been Inserted successfully'));
return $response;
*/
return $this->redirect($this->generateUrl('view_name'));
}
return $this->render('MyBundle:mine:form.html.twig', array('form' => $form->createView()));
}
/**
* @Route("/view", name="view_name")
*/
public function viewAction(Request $request)
{
$userdata = $this->getDoctrine()
->getRepository('MyBundle:school')
->findAll();
return $this->render('MyBundle:mine:view.html.twig', array('view' => $userdata));
//return $userdata;
}
这是我的form.html.twig文件,我使用了form_widget方法。
**form.html.twig**
{% extends 'base.html.twig' %}
{% block body %}
<h1 class="head">STUDENT REGISTRATION FORM</h1>
{{ form_start(form, {'attr': {'id': 'form_person_edit'}}) }}
{{ form_widget(form) }}
<!--{{ form_widget(form, { 'attr': {'class': 'form-group'} }) }}-->
<button type="submit" id="create" class="btn btn-success">Create</button>
{{ form_end(form) }}
{% endblock %}
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('bundles/app/css/style.css') }}" >
<link rel="stylesheet" href="{{ asset('bundles/app/css/jquery-ui.css') }}" >
{% endblock %}
{% block javascripts %}
<script src="{{ asset('bundles/app/js/jquery-3.2.1.min.js') }}"></script>
<script src="{{ asset('bundles/app/js/bootstrap.min.js') }}"></script>
<script src="{{ asset('bundles/app/js/jquery-ui.js') }}"></script>
<script src="{{ asset('bundles/app/js/demo.js') }}"></script>
<script>
var formURL="{{path('form')}}";
var viewURL="{{path('view_name')}}";
</script>
{% endblock %}
这是我的view.html.twig文件,它显示“C:\ xampp \ tmp \ php14FE.tmp”,但我给了.jpg格式,建议我在symfony上传文件更好的方法。我已经看到很多上传方法,但我不知道为什么我的数据库采用.temp文件。
**view.html.twig**
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset('bundles/app/css/bootstrap.css') }}" >
<link rel="stylesheet" href="{{ asset('bundles/app/css/bootstrap-grid.css') }}" >
<link rel="stylesheet" href="{{ asset('bundles/app/css/bootstrap-theme.css') }}" >
<link rel="stylesheet" href="{{ asset('bundles/js/media/css/dataTables.bootstrap.css') }}" >
<link rel="stylesheet" href="{{ asset('bundles/js/media/css/jquery.dataTables.css') }}" >
{% endblock %}
{% block javascripts %}
<script src="{{ asset('bundles/app/js/jquery-3.2.1.min.js') }}"></script>
<script src="{{ asset('bundles/app/js/bootstrap.min.js') }}"></script>
<script src="{{ asset('bundles/app/js/jquery-ui.js') }}"></script>
<script src="{{ asset('bundles/app/js/demo.js') }}"></script>
<script src="{{ asset('bundles/js/media/js/jquery.dataTables.js') }}"></script>
<script src="{{ asset('bundles/js/media/js/dataTables.bootstrap.js') }}"></script>
<script src="{{ asset('bundles/js/media/js/jquery.dataTables.js') }}"></script>
{% endblock %}
{% block body %}
<div class="container" >
<h2>STUDENT INFORMATION FORM</h2>
<table id="table_view" border="">
<thead>
<tr>
<th >USER NAME</th>
<th >FIRST NAME</th>
<th >LAST NAME</th>
<th >EMAIL ID</th>
<th >PASSWORD</th>
<th >UPLOADED FILE</th>
<th >EDIT</th>
<th >DELETE</th>
</tr>
</thead>
<tbody>
{% for key in view %}
<tr>
<td>{{ key.UserName }}</td>
<td>{{ key.FirstName }}</td>
<td>{{ key.LastName }}</td>
<td>{{ key.EmailId }}</td>
<td>{{ key.Password }}</td>
<td>{{ key.UploadFile }}</td>
<td><span class="link"><a href="/my_project/web/app_dev.php/edit/{{ key.id }}" class="btn btn-primary btn-md">Edit</a><span></td>
<td><span class="link"><a href="/my_project/web/app_dev.php/delete/{{ key.id }}" class="deleteUser btn btn-danger btn-md">Delete</a><span></td>
</tr>
<!--<span class=""><i class="glyphicon glyphicon-envelope" aria-hidden="true"></i></span>-->
{% endfor %}
</tbody>
</table>
</div>
<script>
$(document).ready(function(){
$('#table_view').DataTable();
});
</script>
{% endblock %}