Php 无法获取symfony2中上载文件的文件名

Php 无法获取symfony2中上载文件的文件名,php,symfony,upload,Php,Symfony,Upload,我使用Symfony2跟踪了多个文件上传,并创建了一个实体 /* * @ORM\HasLifecycleCallbacks * @ORM\Entity(repositoryClass="Repair\StoreBundle\Entity\attachmentsRepository") */ class attachments { /** * @var integer * * @ORM\Column(name="id", type="integer"

我使用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上载的文件名。当我发送到实体中的上载函数时,它返回空值。我认为您应该再次阅读。您正确地使用了注释
@ORM\HasLifecycleCallbacks
,但您的代码缺少方法的正确注释。您可以使用
@ORM\PostPersist()
@ORM\PostUpdate()
注释在验证请求并持久化对象后自动调用
upload()
函数。我还建议为每个文件使用一个实体并创建一个关系。这将使正确存储文件变得更加容易和合乎逻辑。

不清楚您的意思,除了代码,你能提供更多信息吗?我想上传多个文件。为此,我需要在尝试使用表单[“文件”]->getData()从控制器检索上传的文件时,获取用户从视图上传到控制器的文件名;当我传递$em=$this->getDoctrine()->getManager()时,它返回空值;在upload函数中,它返回空值,即无法将文件名从控制器传送到实体以便上载。在控制器本身中,它无法从视图中获取文件名