Symfony 更新操作返回错误

Symfony 更新操作返回错误,symfony,doctrine-orm,entity,Symfony,Doctrine Orm,Entity,我创建了一个包含图像/文件字段的实体。此实体中记录的插入和呈现工作正常,但要修改实体的部分或所有字段,update函数返回以下错误: 错误:对非对象调用成员函数move() 所以我假设错误在实体本身的函数moveImage()中,即: /** * @ORM\PostPersist() */ public function moveImage() { if (null === $this->image) { return; } if

我创建了一个包含图像/文件字段的实体。此实体中记录的插入和呈现工作正常,但要修改实体的部分或所有字段,update函数返回以下错误:

错误:对非对象调用成员函数move()

所以我假设错误在实体本身的函数moveImage()中,即:

    /**
 * @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);
}
我不知道是什么问题,如果我猜对了。有什么建议吗

这是实体:

    <?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Ricette
 *
 * @ORM\Table()
 * @ORM\HasLifecycleCallbacks
 * @ORM\Entity(repositoryClass="AppBundle\Repository\RicetteRepository")
 */
class Ricette
{
    /**
     * @var int
     *
     * @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="ingredienti", type="text")
     */
    private $ingredienti;

    /**
     * @var string
     *
     * @ORM\Column(name="procedimento", type="text")
     */
    private $procedimento;

    /**
     * @var integer
     *
     * @ORM\Column(name="quantita", type="integer")
     */
    private $quantita;

    /**
     * @var integer
     *
     * @ORM\Column(name="difficolta", type="integer")
     */
    private $difficolta;

    /**
     * @var string
     *
     * @ORM\Column(name="tempo", type="string", length=50)
     */
    private $tempo;

    /**
     * @var string $image
     * @Assert\File( maxSize = "60000000", mimeTypesMessage = "Perfavore inserisci un'immagine valida!")
     * @ORM\Column(name="image", type="string", length=255, nullable=true)
     */
    private $image;

    /**
     * @ORM\ManyToOne(targetEntity="Tipologia", inversedBy="ricette")
     * @ORM\JoinColumn(name="tipologia_id", referencedColumnName="id")
     */
    protected $tipologia;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set titolo
     *
     * @param string $titolo
     *
     * @return Ricette
     */
    public function setTitolo($titolo)
    {
        $this->titolo = $titolo;

        return $this;
    }

    /**
     * Get titolo
     *
     * @return string
     */
    public function getTitolo()
    {
        return $this->titolo;
    }

    /**
     * Set ingredienti
     *
     * @param string $ingredienti
     *
     * @return Ricette
     */
    public function setIngredienti($ingredienti)
    {
        $this->ingredienti = $ingredienti;

        return $this;
    }

    /**
     * Get ingredienti
     *
     * @return string
     */
    public function getIngredienti()
    {
        return $this->ingredienti;
    }

    /**
     * Set procedimento
     *
     * @param string $procedimento
     *
     * @return Ricette
     */
    public function setProcedimento($procedimento)
    {
        $this->procedimento = $procedimento;

        return $this;
    }

    /**
     * Get procedimento
     *
     * @return string
     */
    public function getProcedimento()
    {
        return $this->procedimento;
    }

    /**
     * Set quantita
     *
     * @param integer $quantita
     *
     * @return Ricette
     */
    public function setQuantita($quantita)
    {
        $this->quantita = $quantita;

        return $this;
    }

    /**
     * Get quantita
     *
     * @return integer
     */
    public function getQuantita()
    {
        return $this->quantita;
    }

    /**
     * Set difficolta
     *
     * @param integer $difficolta
     *
     * @return Ricette
     */
    public function setDifficolta($difficolta)
    {
        $this->difficolta = $difficolta;

        return $this;
    }

    /**
     * Get difficolta
     *
     * @return int
     */
    public function getDifficolta()
    {
        return $this->difficolta;
    }

    /**
     * Set tempo
     *
     * @param string $tempo
     *
     * @return Ricette
     */
    public function setTempo($tempo)
    {
        $this->tempo = $tempo;

        return $this;
    }

    /**
     * Get tempo
     *
     * @return string
     */
    public function getTempo()
    {
        return $this->tempo;
    }

/**
     * 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/immaginiRicette/';
    }

    /**
     * @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);
    }

    /**
     * @ORM\PreRemove()
     */
    public function removeImage()
    {
        $file=$this->image;
        if($file = $this->getFullImagePath() && file_exists($file)) {
        unlink($file);
        rmdir($this->getUploadRootDir());
    }
    }

    /**
     * Set tipologia
     *
     * @param \AppBundle\Entity\Tipologia $tipologia
     *
     * @return Ricette
     */
    public function setTipologia(\AppBundle\Entity\Tipologia $tipologia = null)
    {
        $this->tipologia = $tipologia;

        return $this;
    }

    /**
     * Get tipologia
     *
     * @return \AppBundle\Entity\Tipologia
     */
    public function getTipologia()
    {
        return $this->tipologia;
    }
}

已将图像属性映射到字符串。它不能既是上载的文件又是字符串。回到文档:好的,我修复了它!谢谢!:)但现在我有另一个问题:如果我修改了图像数据,当我显示结果(修改了图像字段的记录)时,它不会显示图像,就像它无法找到我上传的新数据一样