我想在Symfony中上传图片,但数据库中的路径为null
。实体中其余的冠军不是null,而值写在Form中。
路径为null,但其余部分(name和updated_at)不为null,并且在表单中写入值。
这是我的Media.php:
<?php
namespace ventenligneBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\validator\Constraints as Assert;
/**
* Media
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="ventenligneBundle\Entity\MediaRepository")
* @ORM\HasLifecycleCallbacks
*/
class Media
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \DateTime
*
* @ORM\COlumn(name="updated_at",type="datetime", nullable=true)
*/
private $updateAt;
/**
* @ORM\PostLoad()
*/
public function postLoad()
{
$this->updateAt = new \DateTime();
}
/**
* @ORM\Column(type="string",length=255)
*/
public $name;
/**
* @ORM\Column(type="string",length=255, nullable=true)
*/
public $path;
public $file;
public function getUploadRootDir()
{
return __dir__.'/../../../web/uploads/produit';
}
public function getAbsolutePath()
{
return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
}
public function getAssetPath()
{
return 'uploads/'.$this->path;
}
/*
*@ORM\PrepPersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
$this->tempFile = $this->getAbsolutePath();
$this->oldFile = $this->getPath();
$this->updateAt = new \DateTime();
if (null !== $this->file)
$this->path = sha1(uniqid(mt_rand(),true)).'.'.$this->file->guessExtension();
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
$this->oldFile = $this->getPath();
if (null !== $this->file) {
$this->file->move($this->getUploadRootDir(),$this->path);
//unset($this->file);
$this->path=$this->getAbsolutePath();
if ($this->oldFile != null) unlink($this->tempFile);
}
}
/**
* @ORM\PreRemove()
*/
public function preRemoveUpload()
{
$this->tempFile = $this->getAbsolutePath();
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if (file_exists($this->tempFile)) unlink($this->tempFile);
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Get name
*
* @return \string
*/
public function getName()
{
return $this->name;
}
/**
* Set updateAt
*
* @param \DateTime $updateAt
* @return Media
*/
public function setUpdateAt($updateAt)
{
$this->updateAt = $updateAt;
return $this;
}
/**
* Get updateAt
*
* @return \DateTime
*/
public function getUpdateAt()
{
return $this->updateAt;
}
/**
* Set name
*
* @param string $name
* @return Media
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
}
Produit.php:
<?php
namespace ventenligneBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Produit
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="ventenligneBundle\Entity\ProduitRepository")
* @UniqueEntity("nom", message=" Une Produit existe déjà avec ce nom.") */
class Produit
{
/**
* @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=125)
*/
private $nom;
/**
* @var string
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* @var float
*
* @ORM\Column(name="prix", type="float")
*/
private $prix;
/**
* @var boolean
*
* @ORM\Column(name="disponible", type="boolean")
*/
private $disponible;
/**
* @var integer
*
* @ORM\Column(name="quantite", type="integer")
*/
private $quantite;
/**
* @ORM\OneToOne(targetEntity="ventenligneBundle\Entity\Media", cascade={"persist"})
*/
private $image;
/**
* @ORM\ManyToOne(targetEntity="Categorie",inversedBy="produit")
* @ORM\JoinColumn(name="id_categorie", referencedColumnName="id")
*/
private $categorie;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set nom
*
* @param string $nom
* @return Produit
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set description
*
* @param string $description
* @return Produit
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set prix
*
* @param float $prix
* @return Produit
*/
public function setPrix($prix)
{
$this->prix = $prix;
return $this;
}
/**
* Get prix
*
* @return float
*/
public function getPrix()
{
return $this->prix;
}
/**
* Set disponible
*
* @param boolean $disponible
* @return Produit
*/
public function setDisponible($disponible)
{
$this->disponible = $disponible;
return $this;
}
/**
* Get disponible
*
* @return boolean
*/
public function getDisponible()
{
return $this->disponible;
}
/**
* Set image
*
* @param \ventenligneBundle\Entity\Media $image
* @return Produit
*/
public function setImage(\ventenligneBundle\Entity\Media $image = null)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return \ventenligneBundle\Entity\Media
*/
public function getImage()
{
return $this->image;
}
/**
* Set tva
*
* @param \ventenligneBundle\Entity\Tva $tva
* @return Produit
*/
public function setTva(\ventenligneBundle\Entity\Tva $tva = null)
{
$this->tva = $tva;
return $this;
}
/**
* Get tva
*
* @return \ventenligneBundle\Entity\Tva
*/
public function getTva()
{
return $this->tva;
}
/**
* Set categorie
*
* @param \ventenligneBundle\Entity\Categorie $categorie
* @return Produit
*/
public function setCategorie(\ventenligneBundle\Entity\Categorie $categorie = null)
{
$this->categorie = $categorie;
return $this;
}
/**
* Get categorie
*
* @return \ventenligneBundle\Entity\Categorie
*/
public function getCategorie()
{
return $this->categorie;
}
/**
* Set quantite
*
* @param integer $quantite
* @return Produit
*/
public function setQuantite($quantite)
{
$this->quantite = $quantite;
return $this;
}
/**
* Get quantite
*
* @return integer
*/
public function getQuantite()
{
return $this->quantite;
}
}
ProduitType.php:
<?php
namespace ventenligneBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use ventenligneBundle\Form\MediaType;
class ProduitType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nom')
->add('description')
->add('prix')
->add('disponible')
->add('quantite')
->add('image' new MediaType())
->add('categorie')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'ventenligneBundle\Entity\Produit'
));
}
/**
* @return string
*/
public function getName()
{
return 'ventenlignebundle_produit';
}
}
这是数据库的屏幕截图:
答案 0 :(得分:0)
我认为您在$file
中实际设置Media.php
时遗失了,因为根本没有制定者:
use Symfony\Component\HttpFoundation\File\UploadedFile;
class Media
{
/**
* @Assert\File(maxSize="20M", mimeTypes={
"application/pdf",
"application/x-pdf",
"application/zip",
"application/x-gzip"
})
*/
public $file;
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
}
我还认识到,getUploadRootDir()
中的路径是:
上传/ produit
但仅限getAssetPath()
:
上传
您也可以使用以下功能制作此DRY:
protected function getUploadDir()
{
return 'uploads/produits';
}
然后重复使用它:
protected function getUploadRootDir()
{
return __DIR__ . '/../../../web/' . $this->getUploadDir();
}
public function getAssetPath()
{
return $this->getUploadDir() . '/' . $this->path;
}
您可以更进一步,通过parameters.yaml
使路径可配置。