在我的Symfony 3.4项目中(vich / uploader-bundle:“ ^ 1.4”),我需要上传3种文件,其中两种文件可以正常工作,但是对于第三种文件,即使我使用相同的过程,它也不会不工作...?提交表单后,我得到了这个例外:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'document_name' cannot be null
当我转储时,我可以看到我的Setter“ vichuploader文档名称”不起作用并且无法持久保存。
这是上下文:
*我的本地版本一切正常...文件已上传,但在我的生产版本中却无法使用。
*我有一个实体(NoteFrais)与另一个实体(Justificatif)具有OneToOne关系。 * Justificatif实体具有@vich \ uploadable注释
我遵循了文档,阅读了很多tuto,在stackverflow上提问,多次清除了缓存并更新了理论架构,尝试了很多事情...
****** config.yml ********
parameters:
locale: fr
app.path.logos: /uploads/logos
app.path.imports: /uploads/imports
app.path.documents: /uploads/documents
vich_uploader:
db_driver: orm
mappings:
logo:
uri_prefix: '%app.path.logos%'
upload_destination: '%kernel.root_dir%/../web%app.path.logos%' //========IT'S WORK FINE FOR THIS ONE==========//
namer: vich_uploader.namer_uniqid
inject_on_load: false
delete_on_update: true
delete_on_remove: true
import:
uri_prefix: '%app.path.imports%'
upload_destination: '%kernel.root_dir%/../web%app.path.imports%' //=========IT'S WORK FINE THIS ONE=========//
namer: vich_uploader.namer_uniqid
# inject_on_load: true important!!
inject_on_load: false
delete_on_update: true
delete_on_remove: true
document:
uri_prefix: '%app.path.documents%'
upload_destination: '%kernel.root_dir%/../web%app.path.documents%'
namer: vich_uploader.namer_uniqid
# inject_on_load: true important!!
inject_on_load: false
delete_on_update: true
delete_on_remove: true
****** JustificatifType *******
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('justificatifFile', FileType::class, array( //========= I TRIED WITH FILETYPE OR VICHFILETYPE =======//
//'data_class' => null, //======== NOTE:USALLY I USE TO FILETYPE AND IT'S WORK FINE ==============//
'label' => false,
'required' => true,
'attr' => array(
'class' => 'NoteFraisBootstrapFileInput',
'type' => 'file',
'placeholder' => 'Selectionner un justificatif (jpeg, png, jpg, pdf)',
'data-preview-file-type' => 'text',
'data-allowed-file-extensions' => '["jpeg", "png", "jpg", "pdf"]',
)
));
}
****** NoteFraisType **********
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
//=======OTHERS FIELDS===========//
->add('justificatif', JustificatifType::class, array(
'required' => false));
}
***** Justificatif.php(实体)*****
<?php
namespace MKG\MystiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* Justificatif
*
* @ORM\Table(name="justificatif")
* @ORM\Entity(repositoryClass="MKG\MystiBundle\Repository\JustificatifRepository")
* @vich\Uploadable
*/
class Justificatif
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="document", fileNameProperty="documentName")
*
* @var File
*/
private $justificatifFile;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $documentName;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
private $updatedAt;
/**
* Constructor
*/
public function __construct()
{
$this->updatedAt = new \DateTime();
}
/**
* 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|UploadedFile $justificatif
*/
public function setJustificatifFile(File $justificatif = null)
{
$this->justificatifFile = $justificatif;
if ($justificatif) {
$this->updatedAt = new \DateTime('now');
}
}
/**
* @return File|null
*/
public function getJustificatifFile()
{
return $this->justificatifFile;
}
/**
*
* @param $documentName
*
* @return $this
*/
public function setDocumentName($documentName)
{
$this->documentName = $documentName;
return $this;
}
/**
* @return string|null
*/
public function getDocumentName()
{
return $this->documentName;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
*
* @return Justificatif
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
}
******* NoteFrais(Entity)*********
<?php
namespace MKG\MystiBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* NoteFrais
*
* @ORM\Table(name="note_frais")
* @ORM\Entity(repositoryClass="MKG\MystiBundle\Repository\NoteFraisRepository")
*/
class NoteFrais
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="MKG\MystiBundle\Entity\Mission", cascade={"persist"})
* @ORM\JoinColumn(name="mission_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
*/
private $mission;
/**
* @ORM\ManyToOne(targetEntity="MKG\MystiBundle\Entity\CodeComptable", cascade={"persist"})
* @ORM\JoinColumn(name="compte_comptable_id", referencedColumnName="id", onDelete="SET NULL", nullable=true)
*/
private $compteComptable;
/**
* @var string
*
* @ORM\Column(name="montant_defraiement_max", type="string", length=255, nullable=false)
*/
private $montantDefraiementMax;
/**
* @var string
*
* @ORM\Column(name="refacturation_client", type="string", length=255, nullable=true)
*/
private $refacturationClient;
/**
* @var string
*
* @ORM\Column(name="total_defraiement", type="string", length=255, nullable=true)
*/
private $totalDefraiement;
/**
* @var string
*
* @ORM\Column(name="total_refacturation", type="string", length=255, nullable=true)
*/
private $totalRefacturation;
/**
* @var string
*
* @ORM\Column(name="compte_avances_et_acomptes", type="string", length=255, nullable=true)
*/
private $compteAvancesEtAcomptes;
/**
* @var string
*
* @ORM\Column(name="admin_current_user", type="string", length=255, nullable=true)
*/
private $currentUser;
/**
* @var string
*
* @ORM\Column(name="code_affaire", type="string", length=255, nullable=true)
*/
private $codeAffaire;
/**
* @var string
*
* @ORM\Column(name="etat", type="string", length=255, nullable=true)
*/
private $etat;
/**
* @ORM\OneToOne(targetEntity="Justificatif", cascade={"persist"})
* @ORM\JoinColumn(name="justificatif_id", referencedColumnName="id", onDelete="CASCADE", nullable=true)
*/
private $justificatif;
/**
* @var \DateTime
*
* @ORM\Column(name="dateCreation", type="datetime")
*/
private $dateCreation;
public function __construct() {
$this->dateCreation = new \DateTime;
$this->etat = "0";
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set montantDefraiementMax
*
* @param string $montantDefraiementMax
*
* @return NoteFrais
*/
public function setMontantDefraiementMax($montantDefraiementMax)
{
$this->montantDefraiementMax = $montantDefraiementMax;
return $this;
}
/**
* Get montantDefraiementMax
*
* @return string
*/
public function getMontantDefraiementMax()
{
return $this->montantDefraiementMax;
}
/**
* Set refacturationClient
*
* @param string $refacturationClient
*
* @return NoteFrais
*/
public function setRefacturationClient($refacturationClient)
{
$this->refacturationClient = $refacturationClient;
return $this;
}
/**
* Get refacturationClient
*
* @return string
*/
public function getRefacturationClient()
{
return $this->refacturationClient;
}
/**
* Set compteAvancesEtAcomptes
*
* @param string $compteAvancesEtAcomptes
*
* @return NoteFrais
*/
public function setCompteAvancesEtAcomptes($compteAvancesEtAcomptes)
{
$this->compteAvancesEtAcomptes = $compteAvancesEtAcomptes;
return $this;
}
/**
* Get compteAvancesEtAcomptes
*
* @return string
*/
public function getCompteAvancesEtAcomptes()
{
return $this->compteAvancesEtAcomptes;
}
/**
* Set currentUser
*
* @param string $currentUser
*
* @return NoteFrais
*/
public function setCurrentUser($currentUser)
{
$this->currentUser = $currentUser;
return $this;
}
/**
* Get currentUser
*
* @return string
*/
public function getCurrentUser()
{
return $this->currentUser;
}
/**
* Set codeAffaire
*
* @param string $codeAffaire
*
* @return NoteFrais
*/
public function setCodeAffaire($codeAffaire)
{
$this->codeAffaire = $codeAffaire;
return $this;
}
/**
* Get codeAffaire
*
* @return string
*/
public function getCodeAffaire()
{
return $this->codeAffaire;
}
/**
* Set etat
*
* @param string $etat
*
* @return NoteFrais
*/
public function setEtat($etat)
{
$this->etat = $etat;
return $this;
}
/**
* Get etat
*
* @return string
*/
public function getEtat()
{
return $this->etat;
}
/**
* Set dateCreation
*
* @param \DateTime $dateCreation
*
* @return NoteFrais
*/
public function setDateCreation($dateCreation)
{
$this->dateCreation = $dateCreation;
return $this;
}
/**
* Get dateCreation
*
* @return \DateTime
*/
public function getDateCreation()
{
return $this->dateCreation;
}
/**
* Set mission
*
* @param \MKG\MystiBundle\Entity\Mission $mission
*
* @return NoteFrais
*/
public function setMission(\MKG\MystiBundle\Entity\Mission $mission = null)
{
$this->mission = $mission;
return $this;
}
/**
* Get mission
*
* @return \MKG\MystiBundle\Entity\Mission
*/
public function getMission()
{
return $this->mission;
}
/**
* Set compteComptable
*
* @param \MKG\MystiBundle\Entity\CodeComptable $compteComptable
*
* @return NoteFrais
*/
public function setCompteComptable(\MKG\MystiBundle\Entity\CodeComptable $compteComptable = null)
{
$this->compteComptable = $compteComptable;
return $this;
}
/**
* Get compteComptable
*
* @return \MKG\MystiBundle\Entity\CodeComptable
*/
public function getCompteComptable()
{
return $this->compteComptable;
}
/**
* Set justificatif
*
* @param \MKG\MystiBundle\Entity\Justificatif $justificatif
*
* @return NoteFrais
*/
public function setJustificatif(\MKG\MystiBundle\Entity\Justificatif $justificatif = null)
{
$this->justificatif = $justificatif;
return $this;
}
/**
* @return File
*/
public function getJustificatif()
{
return $this->justificatif;
}
/**
* Set totalDefraiement
*
* @param string $totalDefraiement
*
* @return NoteFrais
*/
public function setTotalDefraiement($totalDefraiement)
{
$this->totalDefraiement = $totalDefraiement;
return $this;
}
/**
* Get totalDefraiement
*
* @return string
*/
public function getTotalDefraiement()
{
return $this->totalDefraiement;
}
/**
* Set totalRefacturation
*
* @param string $totalRefacturation
*
* @return NoteFrais
*/
public function setTotalRefacturation($totalRefacturation)
{
$this->totalRefacturation = $totalRefacturation;
return $this;
}
/**
* Get totalRefacturation
*
* @return string
*/
public function getTotalRefacturation()
{
return $this->totalRefacturation;
}
}
********* NoteFraisController中的newAction *********
/**
* Creates a new noteFrais entity.
*
*/
public function newAction(Request $request)
{
// === RECUPERE LA MISSION
$em = $this->getDoctrine()->getManager();
$mission = $em->getRepository('MKGMystiBundle:Mission')->findOneBy(array('id' => $request->get('idMission')));
$lignesNoteFrais = $em->getRepository('MKGMystiBundle:NoteFrais')->findBy(array('mission' => $mission->getId()));
$noteFrais = new Notefrais();
$form = $this->createForm('MKG\MystiBundle\Form\NoteFraisType', $noteFrais);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//$noteFrais->setMission($mission);
$em = $this->getDoctrine()->getManager();
$em->persist($noteFrais);
//dump($noteFrais);die();
$em->flush();
$this->addFlash('notice', 'Une ligne a été ajouté à la note de frais!');
return $this->redirect($request->getUri());
}
return $this->render('@MKGMysti/Notefrais/new.html.twig', array(
'lignesNoteFrais' => $lignesNoteFrais,
'mission' => $mission,
'noteFrais' => $noteFrais,
'form' => $form->createView(),
));
}
非常感谢您的时间!!!!