【发布时间】:2014-08-29 11:25:33
【问题描述】:
我有一个实体“任务”和另一个“附件”。我想将所有附件存储在与他们的任务和用户关联的自己的表中。所以我创建了这个实体类:
<?php
namespace Seotool\MainBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="attachments")
*/
class Attachments {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank
*/
public $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="attachments")
* @ORM\JoinColumn(name="user", referencedColumnName="id")
*/
protected $User;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="attachments")
* @ORM\JoinColumn(name="editor", referencedColumnName="id")
*/
protected $Editor;
/**
* @ORM\ManyToOne(targetEntity="Task", inversedBy="attachments")
* @ORM\JoinColumn(name="task", referencedColumnName="id")
*/
protected $Task;
/**
* @Assert\File(maxSize="6000000")
*/
private $file;
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
public function getAbsolutePath()
{
return null === $this->path
? null
: $this->getUploadRootDir().'/'.$this->path;
}
public function getWebPath()
{
return null === $this->path
? null
: $this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads/documents';
}
....
在我的任务表单的表单类型中,我现在要添加文件上传。但是我该怎么做呢?
我无法添加$builder->add('Attachment', 'file');,因为它不是同一个实体。那我该怎么做,以便我在我的实体任务的FormType中有上传字段,该字段将上传的数据存储在实体类附件的表中??
编辑
这是我的控制器:
/**
@Route(
* path = "/taskmanager/user/{user_id}",
* name = "taskmanager"
* )
* @Template()
*/
public function taskManagerAction($user_id, Request $request)
{
/* #### NEW TASK #### */
$task = new Task();
$attachment = new Attachments();
$task->getAttachments()->add($attachment);
$addTaskForm = $this->createForm(new TaskType(), $task);
$addTaskForm->handleRequest($request);
if($addTaskForm->isValid()):
/* User Object of current Users task list */
$userid = $this->getDoctrine()
->getRepository('SeotoolMainBundle:User')
->find($user_id);
$task->setDone(FALSE);
$task->setUser($userid);
$task->setDateCreated(new \DateTime());
$task->setDateDone(NULL);
$task->setTaskDeleted(FALSE);
$attachment->setTask($task);
$attachment->setUser($userid);
$em = $this->getDoctrine()->getManager();
$em->persist($task);
$em->persist($attachment);
$em->flush();
$this->log($user_id, $task->getId(), 'addTask');
return $this->redirect($this->generateUrl('taskmanager', array('user_id' => $user_id)));
endif;
}
【问题讨论】:
标签: php symfony file-upload