Symfony VichUploaderBundle未保存文件-命名字段为空
我有一个应用程序,允许用户在申请课程时上传简历。返回并显示输入表单时不会出现错误。提交表单时,不会抛出任何错误。上传的文件位于POST数据中-作为UploadedFile和cvUpdatedAt字段的实例进行设置。但是,当记录保存到数据库时,用于存储文件名的列被设置为NULL 这是我的实体:Symfony VichUploaderBundle未保存文件-命名字段为空,symfony,vichuploaderbundle,Symfony,Vichuploaderbundle,我有一个应用程序,允许用户在申请课程时上传简历。返回并显示输入表单时不会出现错误。提交表单时,不会抛出任何错误。上传的文件位于POST数据中-作为UploadedFile和cvUpdatedAt字段的实例进行设置。但是,当记录保存到数据库时,用于存储文件名的列被设置为NULL 这是我的实体: <?php namespace My\CamsBundle\Entity; use DateTime; use Doctrine\ORM\Mapping as ORM; use Doctrine\
<?php
namespace My\CamsBundle\Entity;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass="My\CamsBundle\Entity\Repository\ApplicationRepository")
* @ORM\Table(name="application")
* @Vich\Uploadable
* @ORM\HasLifecycleCallbacks()
*/
class Application {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @Assert\File(
* maxSize="2M",
* )
* @Vich\UploadableField(mapping="cv_file", fileNameProperty="cvName")
*
* This is not a mapped field of entity metadata, just a simple property.
*
* @var File $cvFile
*/
protected $cvFile;
/**
* @ORM\Column(type="string", length=255, name="cv_name", nullable=true)
*
* @var string $cvName
*/
protected $cvName;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime $cvUpdatedAt
*/
protected $cvUpdatedAt;
public function __construct() { }
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $cv
*/
public function setCvFile(File $cv = null) {
$this->cvFile = $cv;
if ($cv) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->cvUpdatedAt = new \DateTime('now');
}
}
/**
* @return File
*/
public function getCvFile() {
return $this->cvFile;
}
/**
* @param string $cvName
*/
public function setCvName($cvName) {
$this->cvName = $cvName;
}
/**
* @return string
*/
public function getCvName() {
return $this->cvName;
}
/**
* Set cvUpdatedAt
*
* @param \DateTime $cvUpdatedAt
* @return Application
*/
public function setCvUpdatedAt($cvUpdatedAt) {
$this->cvUpdatedAt = $updatcvUpdatedAt;
return $this;
}
/**
* Get cvUpdatedAt
*
* @return \DateTime
*/
public function getCvUpdatedAt() {
return $this->cvUpdatedAt;
}
}
表单模板:
{% extends 'MyCamsBundle::layout.html.twig' %}
{% form_theme form 'MyCamsBundle:Form:fields.html.twig' %}
{% block body %}
<div class="main">
{{ form_errors(form) }}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div class="heading">Application</div>
</div>
</div>
<section id="application-form">
{{ form_start(form) }}
<div class="row well demonstrator-experience">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 cv-details">
<span class="btn btn-action btn-file">
{{ form_label(form.cvFile) }} {{ form_widget(form.cvFile) }}
</span>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 text-right">
</div>
{{ form_widget(form.save) }}
</div>
{{ form_end(form) }}
</section>
</div>
{% endblock %}
{%extends'MyCamsBundle::layout.html.twig%}
{%form_主题表单'MyCamsBundle:form:fields.html.twig%}
{%block body%}
{{form_errors(form)}}
应用
{{form_start(form)}}
{{form_标签(form.cvFile)}{{form_小部件(form.cvFile)}}
{{form_小部件(form.save)}
{{form_end(form)}}
{%endblock%}
控制器:
<?php
namespace My\CamsBundle\Controller;
use DateTime;
use Doctrine\ORM\EntityManager;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use My\CamsBundle\Entity\Application;
use My\CamsBundle\Form\ApplicationType;
/**
* Apply for a casual position.
*
* Class ApplicationController
* @package My\CamsBundle\Controller
*/
class ApplicationController extends Controller
{
/**
* Application form.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*
* @Route("/application/create")
*/
public function createAction()
{
$application = new Application();
$em = $this->getDoctrine()->getManager();
$form = $this->createFormModel($application);
return $this->render('MyCamsBundle:Application:create.html.twig', array(
'form' => $form->createView(),
));
}
/**
* Application save form.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*
* @Route("/application/save")
*/
public function saveAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$raw_application = $request->get('application');
$request->request->set('application', $raw_application);
$application = new Application();
$form = $this->createFormModel($application);
$form->handleRequest($request);
$application_id = 0;
try {
if ($form->isValid()) {
$application = $form->getData();
$em->merge($application);
$em->flush();
$application_id = $application->getId();
} else {
return $this->render('MyCamsBundle:Application:create.html.twig', array(
'form' => $form->createView(),
));
}
} catch (Exception $e) {
$request->getSession()->getFlashBag()->add(
'notice', 'Failed to save changes'
);
$logger = $this->get('logger');
$logger->error($e->getMessage());
return $this->render('MyCamsBundle:Application:create.html.twig', array(
'form' => $form->createView(),
));
}
return $this->redirect($this->generateUrl('my_cams_application_update'));
}
/**
* Application edit form. This is the applicant's dashboard.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*
* @Route("/application/update")
*/
public function updateAction()
{
$em = $this->getDoctrine()->getManager();
// etc.
}
/**
* @param $application
* @return \Symfony\Component\Form\Form
*/
private function createFormModel($application)
{
$form = $this->createForm(new ApplicationType(), $application, array(
'action' => $this->generateUrl('my_cams_application_save')
)
);
return $form;
}
}
不确定这是否有帮助,但会更改:$cvUpdatedAt
和$updatedAt
。就我而言,它起了作用 欢迎来到堆栈溢出!这看起来比严格需要的代码还要多。您是否可以进一步减少代码,直到仍然遇到问题时无法删除任何代码?(另外,代码段标记是完全错误的;只保留代码块。)您使用的是什么版本的vich/uploader捆绑包?我无法使用最新版本,必须使用v0.14.0。与1.0相比,此版本运行良好。*@dev。也许这会有所帮助。将此“vich/uploader捆绑包”:“0.14.0”
添加到VichUploader的composer.json.Version 1.0.x-dev中。已经搬到VlabsMediaBundle。
{% extends 'MyCamsBundle::layout.html.twig' %}
{% form_theme form 'MyCamsBundle:Form:fields.html.twig' %}
{% block body %}
<div class="main">
{{ form_errors(form) }}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div class="heading">Application</div>
</div>
</div>
<section id="application-form">
{{ form_start(form) }}
<div class="row well demonstrator-experience">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 cv-details">
<span class="btn btn-action btn-file">
{{ form_label(form.cvFile) }} {{ form_widget(form.cvFile) }}
</span>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 text-right">
</div>
{{ form_widget(form.save) }}
</div>
{{ form_end(form) }}
</section>
</div>
{% endblock %}
<?php
namespace My\CamsBundle\Controller;
use DateTime;
use Doctrine\ORM\EntityManager;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use My\CamsBundle\Entity\Application;
use My\CamsBundle\Form\ApplicationType;
/**
* Apply for a casual position.
*
* Class ApplicationController
* @package My\CamsBundle\Controller
*/
class ApplicationController extends Controller
{
/**
* Application form.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*
* @Route("/application/create")
*/
public function createAction()
{
$application = new Application();
$em = $this->getDoctrine()->getManager();
$form = $this->createFormModel($application);
return $this->render('MyCamsBundle:Application:create.html.twig', array(
'form' => $form->createView(),
));
}
/**
* Application save form.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*
* @Route("/application/save")
*/
public function saveAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$raw_application = $request->get('application');
$request->request->set('application', $raw_application);
$application = new Application();
$form = $this->createFormModel($application);
$form->handleRequest($request);
$application_id = 0;
try {
if ($form->isValid()) {
$application = $form->getData();
$em->merge($application);
$em->flush();
$application_id = $application->getId();
} else {
return $this->render('MyCamsBundle:Application:create.html.twig', array(
'form' => $form->createView(),
));
}
} catch (Exception $e) {
$request->getSession()->getFlashBag()->add(
'notice', 'Failed to save changes'
);
$logger = $this->get('logger');
$logger->error($e->getMessage());
return $this->render('MyCamsBundle:Application:create.html.twig', array(
'form' => $form->createView(),
));
}
return $this->redirect($this->generateUrl('my_cams_application_update'));
}
/**
* Application edit form. This is the applicant's dashboard.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*
* @Route("/application/update")
*/
public function updateAction()
{
$em = $this->getDoctrine()->getManager();
// etc.
}
/**
* @param $application
* @return \Symfony\Component\Form\Form
*/
private function createFormModel($application)
{
$form = $this->createForm(new ApplicationType(), $application, array(
'action' => $this->generateUrl('my_cams_application_save')
)
);
return $form;
}
}
<?php
namespace My\CamsBundle\Entity\Repository;
use Doctrine\ORM\EntityRepository;
/**
* ApplicationRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class ApplicationRepository extends EntityRepository
{
}