I have one entity "Task" and another "Attachments". I want to store all attachments in their own table associated with their task and user. So I created this entity Class:
<?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';
}
....
In my Form Type for my Task Form I want to add now the file upload. But how can I do this?
I can't add $builder->add('Attachment', 'file');
because it's not the same entity. So how can I do it, so that I have in my FormType of Entity Task the upload field which stores the uploaded data in the table of Entity Class Attachment??
EDIT
this is my Controller:
/**
@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;
}