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

4

3 回答 3

0

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

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 解决了这个问题,而不是我。我只是想传播爱。

于 2013-01-09T16:18:17.473 回答
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>');
    }
  }
于 2013-02-15T23:03:11.183 回答
0

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

于 2012-08-03T13:14:41.817 回答