我遵循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
}
}
}
控制器无法从视图中获取用户上传的文件名。当我发送到实体
我想你应该再读一遍关于文件上传的文档。您正确地使用了注释@ORM'HasLifecycleCallbacks
,但是您的代码缺少方法的正确注释。您可以使用@ORM'PostPersist()
和@ORM'PostUpdate()
注释在验证请求并持久化对象之后自动调用upload()
函数。我还建议为每个文件使用一个实体,并创建一个OneToMany关系。