无法在symfony2中获取上传文件的文件名

时间:2013-05-27 05:09:29

标签: php symfony upload

我已经使用Symfony2跟踪多个文件上传并创建了一个实体

/*
 * @ORM\HasLifecycleCallbacks
 * @ORM\Entity(repositoryClass="Repair\StoreBundle\Entity\attachmentsRepository")
 */

class attachments
{

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */

    private $id;
    /**
     * @var string
     * 
     * @Assert\File(maxSize="6000000")
     * @ORM\Column(name="files", type="array", length=255, nullable=true)
     */
    private $files=array();

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

    /**
     * Set files
     * @param object $files
     * 
     * @return attachments
     */
    public function setFiles($files) {
        $this->files = $files;
    }

    /**
     * Get files
     *
     * @return object 
     */
    public function getFiles() {
        return $this->files;
    }

    public function __construct() {
        $files = array();
    }

     public function uploadFiles() {
        // the files property can be empty if the field is not required
        if (null === $this->files) {
            return;
        } 

        if (!$this->id) {
                $this->files->move($this->getTmpUploadRootDir(), $this->files->getClientOriginalName());

        } else {
            $this->files->move($this->getUploadRootDir(), $this->files->getClientOriginalName());
        }

        $this->setFiles($this->files->getClientOriginalName());
    }

    public function getAbsolutePath() {
        return null === $this->path
            ? null
            : $this->getUploadRootDir() . DIRECTORY_SEPARATOR . $this->path;
    }

    public function getWebPath() {
        return null === $this->path
            ? null
            : $this->getUploadDir() . DIRECTORY_SEPARATOR . $this->path;
    }

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

    protected function getUploadDir() {
        return 'uploads/';
    }

}

我有一个控制器,其中包含以下代码

class uploadController extends Controller
{

    public function uploadAction(Request $request) {

        $id= $_GET['id'];

        $user = new attachments();

        $form = $this->createFormBuilder($user)->add('files','file',array("attr"=>array("multiple" =>"multiple",)))->getForm();

        $formView = $form->createView();

        $formView->getChild('files')->set('full_name','form[file][]');

        if ($request->getMethod() == 'POST') {

            $em = $this->getDoctrine()->getManager();
            $form->bind($request);
            $files = $form["files"]->getFilenames();
            $user->uploadFiles(); //--here iam not able to get te file names in order to send to the upload function.upload files is returning null values

       }
   }
}

控制器无法从view中获取uder上传的文件名。当我发送到实体中的上传函数时,它返回空值

1 个答案:

答案 0 :(得分:0)

我认为你应该再次阅读Documentation about File Upload。您正确使用注释@ORM\HasLifecycleCallbacks,但您的代码缺少方法的正确注释。在验证请求并保留对象后,您可以使用@ORM\PostPersist()@ORM\PostUpdate()注释自动调用upload()函数。另外,我建议为每个文件使用一个实体并创建OneToMany关系。这样可以更加轻松合理地存储文件。