Symfony形式。上传文件

时间:2012-07-28 20:45:50

标签: forms upload entity symfony-2.1

尝试使用Entity管理文件上传,但是我收到此错误:

  

致命错误:在第327行的/home/projectpath/src/BS/MyBundle/Entity/Items.php中调用非对象上的成员函数move()调用堆栈:0.0002 333264 1. {main}( )/home/projectpath/web/app_dev.php:0 0.0450 1158160 ......

这是实体类:

namespace BS\BackstretcherBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;

/**
* MB\MyBundle\Entity\Items
*
* @ORM\Table(name="items")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class Items
{
    private $filenameForRemove;

    /**
     * @Assert\File(maxSize="60000000")
     */
    public $file;
    ...

    protected function getUploadDir()
    {
       return 'images/items/';
    }

    protected function getUploadRootDir()
    {
      return __DIR__.'/../../../../web/'.$this->getUploadDir();
    }

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

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

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

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

      $this->file->move($this->getUploadRootDir(), $this->file);
      unset($this->file);
    }

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

控制器:

public function new_productAction(Request $request)
{
  $product = new Items();
  $product->setPrice(0);

  $form = $this->createFormBuilder($product)
    ->add('Type', 'choice', array(
      'choices'   => array('1' => 'Product', '0' => 'Article'),
      'required'  => false,))
    ->add('Price',  'number')
    ->add('nameEn', 'text')
    ->add('file', 'file', array('label' => 'Image', 'required' => true))
    ->getForm();

  if ($request->getMethod() == 'POST')
  {
    if ($form->isValid())
    {
      $form->bindRequest($request);
      $em = $this->getDoctrine()->getEntityManager();
      $em->persist($product);
      $em->flush();

      return new Response('<html><body>Success!</body></html>');
    }
  }

  return $this->render('MyBundle:Default:admin_page.html.twig', array(
    'form' => $form->createView(),
  ));
}

Symfony版本:2.1.0

3 个答案:

答案 0 :(得分:0)

检查你的php.ini文件,确保post_max_size和upload_max_filesize都设置得足够大。

答案 1 :(得分:0)

我不认为duke_nukem再担心这个问题了,6个月后,但是如果有其他人遇到这个问题,我遇到了同样的问题并且在这里得到了很好的答案:

Error with file upload in symfony 2

看起来像duke_nukem和我犯了同样的错误。 preUpload()方法应为:

  /**
   * @ORM\PrePersist()
   * @ORM\PreUpdate()
   */
    public function preUpload()
    {
      if (null !== $this->file)
      {
         $this->path = $this->getId() .'.'. $this->file->guessExtension();
      }
    }

当前代码将$this->file转换为字符串,从而导致错误。实际上应该将路径分配给$this->path

Sybio在另一个问题中找到了这个,而不是我。我只想传播爱情。

答案 2 :(得分:0)

这很奇怪

您的控制器中的代码错误。在验证之前,您必须将您的请求绑定到表单。之后,您可以检索数据

if ($request->getMethod() == 'POST')
  {
    //Note: bindRequest is now deprecated
    $form->bind($request);
    if ($form->isValid())
    {
      //retrieve your model hydrated with your form values
      $product = $form->getData();

      //has upload file ?
      if($product->getFile() instanceof UploadedFile){
        //you can do your upload logic here wihtout doctrine event if you want
      }

      $em = $this->getDoctrine()->getEntityManager();
      $em->persist($product);
      $em->flush();

      return new Response('<html><body>Success!</body></html>');
    }
  }