-1

我已经使用 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

       }
   }
}

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

4

1 回答 1

0

我认为您应该再次阅读有关文件上传的文档。您正确使用了注释@ORM\HasLifecycleCallbacks,但您的代码缺少方法的正确注释。在验证请求并持久化对象后,您可以使用@ORM\PostPersist()@ORM\PostUpdate()注释自动调用该函数。upload()另外我建议为每个文件使用一个实体并创建一个OneToMany关系。这将使正确存储文件变得更加容易和合乎逻辑。

于 2013-05-27T17:20:00.607 回答