使用文件字段更新实体

时间:2015-12-16 11:30:46

标签: 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()
 * @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;
    }
}

在控制器中我有更新动作

这是控制器行动

public function update_ricettaAction(Request $request, $id)
{
    //$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Non puoi accedere a questa pagina!');


    $em = $this->getDoctrine()->getManager();
    $recipe = $em->getRepository('AppBundle:Recipe')->find($id);
    $form = $this->createForm(new RecipeType($recipe), $recipe);
    $form->handleRequest($request);
    if ($form->isValid())
    {
        $em = $this->getDoctrine()->getManager();
        try
        {
            $em->persist($recipe);
            $em->flush();

            return $this->redirect($this->generateUrl('successricettaupdate'));
        } catch (\Exception $e)
        {

            $form->addError(new FormError('errore nel database: ' . $e->getMessage()));
        }

        if ($form->isValid())
        {
            $var = $recipe;


            $em->persist($var);
            $em->flush();

            return $this->redirect($this->generateUrl('successricettaupdate'));
        } else
        {

        }
    }

    return $this->render('administration/update_ricetta.html.twig', array(
                'recipe' => $recipe,
                'form' => $form->createView()));
}

当我提交表单时,要更新实体的全部,部分或仅一个字段,我会收到错误:

  

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

我不知道它能是什么...... 有什么建议吗?

1 个答案:

答案 0 :(得分:0)

<强>解决

我解决了自己的问题,如果可以帮助任何人,这就是解决方案:

在实体中,我对此进行了修改:

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

}

现在我没有收到任何错误,更新有效!