我正在使用Symfony 2.7开发一个网站!我刚开始学习它,我正在尝试上传文件,文件名保存在我的数据库中,但它没有上传到目录中 任何人可以帮我这里是我的控制器和实体: 控制器:
namespace institutionBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use institutionBundle\Entity\Classe;
use institutionBundle\Form\ClasseType;
use institutionBundle\Entity\Niveau;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
/**
* Classe controller.
*
*/
class ClasseController extends Controller
{
public function newAction(request $request,$institut)
{
$em = $this->getDoctrine()->getManager();
$classe = new Classe();
$form = $this->createFormBuilder($classe)
->add('nom','text',array('required'=>true,'attr'=>array('class'=>'form-control','placeholder'=>'Entrez le nom')))
->add('niveau', 'entity', array(
'attr'=>array('class'=>'form-control'),
'class' => 'institutionBundle:Niveau',
'property' => 'nom',
'query_builder' => function (EntityRepository $er)use ($institut) {
return $er->createQueryBuilder('i')
->andWhere('i.institut = :niv')->setParameter('niv', $institut);
}
))
->add('file')
->add('Ajouter','submit',array('attr'=>array('class'=>'btn btn-success of')))
->getForm();
$form->handleRequest($request);
$alert='';
if ($form->isSubmitted() && $form->isValid()) {
$x = $form->get('nom')->getData();
$d = $em->createQuery("select d from institutionBundle:Classe d where (d.nom = :niv and d.institut = :inst )")
->setParameters(array('niv'=>$x,'inst'=>$institut))
->getResult();
$num2 = count($d);
if($num2>0){
$alert ='Cette Classe existe déjà! Veuillez choisir un autre nom';
}else{
$inst = $em->getRepository("institutionBundle:Institut")->find($institut);
$classe->upload();
$classe->setInstitut($inst);
$mats=$form->get('niveau')->getData();
foreach ($mats as $mat) {
$s = $em->getRepository("institutionBundle:Niveau")->find($mat);
$classe->setNiveau($s);
}
$em-> persist($classe);
$em->flush();
return $this->redirectToRoute('home_classes',array('institut'=>$institut));
}
}
return $this->render('institutionBundle:Classe:new.html.twig', array(
'form' => $form->createView(),'alert'=>$alert,'institut'=>$institut
));
}
}
实体:
<?php
namespace institutionBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Classe
*
* @ORM\Table()
* @ORM\Entity
*/
class Classe
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nom", type="string", length=255)
*/
private $nom;
/**
*
* @ORM\ManyToOne(targetEntity="institutionBundle\Entity\Niveau",inversedBy="id")
* @ORM\JoinColumn(name="niveau",referencedColumnName="id")
*/
private $niveau;
/**
*
* @ORM\ManyToOne(targetEntity="institutionBundle\Entity\Institut",inversedBy="id")
* @ORM\JoinColumn(name="institut",referencedColumnName="id")
*/
private $institut;
/**
* @Assert\File(maxSize="6000000")
*/
private $file;
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
public function getAbsolutePath()
{
return null === $this->path
? null
: $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path
? null
: $this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads';
}
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->getFile()) {
return;
}
$this->getFile()->move(
$this->getUploadRootDir(),
$this->getFile()->getClientOriginalName()
);
$this->path = $this->getFile()->getClientOriginalName();
// clean up the file property as you won't need it anymore
$this->file = null;
}
/**
* Set path
*
* @param string $path
*
* @return Stage
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set matiere
*
* @param string $institut
* @return Professeur
*/
public function setInstitut($inst)
{
$this->institut = $inst;
return $this;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $nom
* @return Classe
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Get dateProchain
*
* @return \DateTime
*/
public function getNiveau()
{
return $this->niveau;
}
/**
* Set dateProchain
*
* @param \DateTime $dateProchain
* @return Eleve
*/
public function setNiveau($niveau)
{
$this->niveau = $niveau;
return $this;
}
}
答案 0 :(得分:0)
我会说你错过了Uploader服务。 或许你没有在你的问题上发帖。
这可以帮到你: Symfony 2.7 Uploader Service
那里的代码与你的代码非常相似,所以应该很容易理解并转化为你的问题
答案 1 :(得分:0)
我找到了解决方案
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../web/'.$this->getUploadDir();
}