drc15469 2014-08-29 11:25
浏览 33
已采纳

使用自己的实体上传Symfony2文件

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;
}
  • 写回答

3条回答 默认 最新

查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM