Forms 使用文件字段更新实体
我正在尝试更新包含文件字段的配方实体,尤其是图像 这是我的实体Forms 使用文件字段更新实体,forms,symfony,updates,Forms,Symfony,Updates,我正在尝试更新包含文件字段的配方实体,尤其是图像 这是我的实体 <?php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Doctrine\Common\Collections\ArrayCollection; /** * Article * * @ORM\Table() *
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Article
*
* @ORM\Table()
* @ORM\HasLifecycleCallbacks
* @ORM\Entity
*/
class Article
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="titolo", type="string", length=255)
*/
private $titolo;
/**
* @var string
*
* @ORM\Column(name="autore", type="string", length=255)
*/
private $autore;
/**
* @var string
*
* @ORM\Column(name="testo", type="text")
*/
private $testo;
/**
* @var string
*
* @ORM\Column(name="categoria", type="string", length=100)
*/
private $categoria;
/**
* @var string $image
* @Assert\File( maxSize = "1024k", mimeTypesMessage = "Perfavore inserisci un'immagine valida!")
* @ORM\Column(name="image", type="string", length=255, nullable=true)
*/
private $image;
/**
* @var date
*
* @ORM\Column(name="data", type="date")
*/
public $data;
/**
* @var integer
*
* @ORM\Column(name="rate", type="integer",nullable=true)
*/
private $rate;
/**
* @ORM\OneToMany(targetEntity="CommentoArticle", mappedBy="article")
*/
protected $commentoarticle;
public function __construct()
{
$this->commentoarticle = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set titolo
*
* @param string $titolo
*
* @return Article
*/
public function setTitolo($titolo)
{
$this->titolo = $titolo;
return $this;
}
/**
* Get titolo
*
* @return string
*/
public function getTitolo()
{
return $this->titolo;
}
/**
* Set autore
*
* @param string $autore
*
* @return Article
*/
public function setAutore($autore)
{
$this->autore = $autore;
return $this;
}
/**
* Get autore
*
* @return string
*/
public function getAutore()
{
return $this->autore;
}
/**
* Set testo
*
* @param string $testo
*
* @return Article
*/
public function setTesto($testo)
{
$this->testo = $testo;
return $this;
}
/**
* Get testo
*
* @return string
*/
public function getTesto()
{
return $this->testo;
}
/**
* Set image
*
* @param string $image
*/
public function setImage($image)
{
$this->image = $image;
}
/**
* Get image
*
* @return string
*/
public function getImage()
{
return $this->image;
}
public function getFullImagePath() {
return null === $this->image ? null : $this->getUploadRootDir(). $this->image;
}
protected function getUploadRootDir() {
// the absolute directory path where uploaded documents should be saved
return $this->getTmpUploadRootDir().$this->getId()."/";
}
protected function getTmpUploadRootDir() {
// the absolute directory path where uploaded documents should be saved
return __DIR__ . '/../../../web/imgArticoli/';
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function uploadImage() {
// the file property can be empty if the field is not required
if (null === $this->image) {
return;
}
if(!$this->id){
$this->image->move($this->getTmpUploadRootDir(), $this->image->getClientOriginalName());
}else{
$this->image->move($this->getUploadRootDir(), $this->image->getClientOriginalName());
}
$this->setImage($this->image->getClientOriginalName());
}
/**
* @ORM\PostPersist()
*/
public function moveImage()
{
if (null === $this->image) {
return;
}
if(!is_dir($this->getUploadRootDir())){
mkdir($this->getUploadRootDir());
}
copy($this->getTmpUploadRootDir().$this->image, $this->getFullImagePath());
unlink($this->getTmpUploadRootDir().$this->image);
}
/**
* Set data
*
* @param \DateTime $data
*
* @return Article
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
/**
* Get data
*
* @return \DateTime
*/
public function getData()
{
return $this->data;
}
/**
* Set categoria
*
* @param string $categoria
*
* @return Article
*/
public function setCategoria($categoria)
{
$this->categoria = $categoria;
return $this;
}
/**
* Get categoria
*
* @return string
*/
public function getCategoria()
{
return $this->categoria;
}
/**
* Add commentoarticle
*
* @param \AppBundle\Entity\CommentoArticle $commentoarticle
*
* @return Article
*/
public function addCommentoArticle(\AppBundle\Entity\CommentoArticle $commentoarticle)
{
$this->commentoarticle[] = $commentoarticle;
return $this;
}
/**
* Remove commentoarticle
*
* @param \AppBundle\Entity\CommentoArticle $commentoarticle
*/
public function removeCommentoArticle(\AppBundle\Entity\CommentoArticle $commentoarticle)
{
$this->commentoarticle->removeElement($commentoarticle);
}
/**
* Get commentoarticle
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCommentoArticle()
{
return $this->commentoarticle;
}
/**
* Set rate
*
* @param integer $rate
*
* @return Article
*/
public function setRate($rate)
{
$this->rate = $rate;
return $this;
}
/**
* Get rate
*
* @return integer
*/
public function getRate()
{
return $this->rate;
}
}
提交表单时,若要更新实体的全部、部分或仅一个字段,则会出现以下错误:
错误:对非对象调用成员函数move()
我不知道会是什么。。。
有什么建议吗?已解决 我解决了自己的问题,如果你能帮助任何人,这就是解决方案: 在实体中,我修改了以下内容:
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function uploadImage() {
// the file property can be empty if the field is not required
if (null === $this->image) {
return;
}
if(!$this->id){
$this->image->move($this->getTmpUploadRootDir(), $this->image->getClientOriginalName());
}else{
$this->image->move($this->getUploadRootDir(), $this->image->getClientOriginalName());
}
$this->setImage($this->image->getClientOriginalName());
}
为此:
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function uploadImage() {
// the file property can be empty if the field is not required
if (null === $this->image) {
return;
}
if(!$this->id){
$this->image->move($this->getTmpUploadRootDir(), $this->image->getClientOriginalName());
}else{
return null;
}
}
现在我没有收到任何错误,更新工作 您的表单是否包含
enctype=“multipart/form data”
?我有这样一个:method=“post”{{{form\u enctype(form)}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function uploadImage() {
// the file property can be empty if the field is not required
if (null === $this->image) {
return;
}
if(!$this->id){
$this->image->move($this->getTmpUploadRootDir(), $this->image->getClientOriginalName());
}else{
return null;
}