LifecycleCallbacks不在Symfony2中执行

时间:2015-05-30 20:44:56

标签: symfony doctrine-orm

即使我有LifecycleCallbacks注释,symfony2 @ORM\HasLifecycleCallbacks中的The file could not be found.也没有被执行。我想按照http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html#using-the-id-as-the-filename中提供的示例进行操作。我的目标是将文件保存在文档ID下。

  • 以下提供的代码不会导致错误,除了 重新加载页面中的die("TEST");信息。
  • 要检查发生了什么,我将upload() function命令添加到/** * @Route("app/documents/add/", name="app_documents_add") */ public function addAction(Request $request) { /** * This code is aimed at checking if the book is choseen and therefore whether any further works may be carried out */ $session = new Session(); if(!$session->get("App_Books_Chosen_Lp")) return new RedirectResponse($this->generateUrl('app_listbooks')); // Authorization goes here $documents = new Documents(); $form = $this->createForm(new DocumentsType(), $documents); $form->add('save', 'submit', array('label' => 'Dodaj dokument')); $form->handleRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); //$documents->upload(); $book = $em->getReference('AppBundle:Books', $session->get("App_Books_Chosen_Lp")); if( $book ) $documents->setBook($book); else die ("CRITICAL ERROR: addAction - Bad book id"); $em->persist($documents); $em->flush(); } return $this->render('AppBundle:Documents:adddocuments.html.twig', array('form' => $form->createView())); } 但似乎它永远不会被执行,因为结果只是表单页面被重新加载了上述错误。

我想问你一些建议可能是什么原因导致upload()函数没有被执行?

控制器:

<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 * @ORM\Table(name="Documents")
 */

class Documents
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="Books", inversedBy="documents")
     * @ORM\JoinColumn(name="book_id", referencedColumnName="id")
     */
    protected $book;

    /**
     * @ORM\Column(type="string", length=220)
     */
    protected $marker;

    /**
     * @ORM\Column(type="date", length=220)
     */
    protected $document_date;

    /**
     * @ORM\Column(type="string", length=220)
     * @Assert\File(maxSize="6000000")
     */
     protected $link;

     /**
     * @ORM\Column(type="text")
     */
     protected $notes;


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

    /**
     * Set marker
     *
     * @param string $marker
     * @return Documents
     */
    public function setMarker($marker)
    {
        $this->marker = $marker;

        return $this;
    }

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

    /**
     * Set document_date
     *
     * @param \DateTime $documentDate
     * @return Documents
     */
    public function setDocumentDate($documentDate)
    {
        $this->document_date = $documentDate;

        return $this;
    }

    /**
     * Get document_date
     *
     * @return \DateTime 
     */
    public function getDocumentDate()
    {
        return $this->document_date;
    }

    /**
     * Set link
     *
     * @param string $link
     * @return Documents
     */
    public function setLink($link)
    {
        $this->link = $link;

        return $this;
    }

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


    /**
     * Set notes
     *
     * @param string $notes
     * @return Documents
     */
    public function setNotes($notes)
    {
        $this->notes = $notes;

        return $this;
    }

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

    /**
     * Set book
     *
     * @param \AppBundle\Entity\Books $book
     * @return Documents
     */
    public function setBook(\AppBundle\Entity\Books $book = null)
    {
        $this->book = $book;

        return $this;
    }

    /**
     * Get book
     *
     * @return \AppBundle\Entity\Books 
     */
    public function getBook()
    {
        return $this->book;
    }

    /*
    * ### FILE UPLOAD PROCESS ### 
    */

    /**
     * @Assert\File(maxSize="6000000")
     */
    private $file;

     public function getWebPath()
    {
        return null === $this->link
            ? null
            : $this->getUploadDir().'/'.$this->link;
    }

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

     /**
     * Get file.
     *
     * @return UploadedFile
     */
    public function getFile()
    {
        return $this->file;
    }

    /* 
    * Temp fila path
    */

    private $temp;


    /**
     * Sets file.
     *
     * @param UploadedFile $file
     */
    public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;
        // check if we have an old image path
        if (is_file($this->getAbsolutePath())) {
            // store the old name to delete after the update
            $this->temp = $this->getAbsolutePath();
        } else {
            $this->link = 'initial';
        }
    }

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->getFile()) {
            $this->link = $this->getFile()->guessExtension();
        }
    }

    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null === $this->getFile()) {
            return;
        }

        // check if we have an old image
        if (isset($this->temp)) {
            // delete the old image
            unlink($this->temp);
            // clear the temp image path
            $this->temp = null;
        }

        // you must throw an exception here if the file cannot be moved
        // so that the entity is not persisted to the database
        // which the UploadedFile move() method does
        $this->getFile()->move(
            $this->getUploadRootDir(),
            $this->id.'.'.$this->getFile()->guessExtension()
        );
        $this->setFile(null);
    }

    /**
     * @ORM\PreRemove()
     */
    public function storeFilenameForRemove()
    {
        $this->temp = $this->getAbsolutePath();
    }

    /**
     * @ORM\PostRemove()
     */
    public function removeUpload()
    {
        if (isset($this->temp)) {
            unlink($this->temp);
        }
    }

    public function getAbsolutePath()
    {
        return null === $this->link
            ? null
            : $this->getUploadRootDir().'/'.$this->id.'.'.$this->link;
    }
}

`

文件类:

-+[] ->/

1 个答案:

答案 0 :(得分:0)

为什么upload()函数没有被执行的答案非常简单 - 表单被标记为无效。现在我正在寻找帮助,为什么会发生这种情况(The file could not be found while using LifecycleCallbacks