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

时间:2014-08-29 11:25:34

标签: php symfony file-upload

我有一个实体"任务"和另一个"附件"。我想将所有附件存储在与其任务和用户关联的自己的表中。所以我创建了这个实体类:

<?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 of Entity Task中有上传字段,它将上传的数据存储在实体类附件表中?

修改

这是我的控制器:

/**
@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 个答案:

答案 0 :(得分:1)

您应该将实体从Attachments重命名为Attachment,因为它只会存储一个附件的数据。

在您的情况下,您需要Symfony2表单集合类型以允许在任务表单中添加附件(TaskType):

$builder->add('attachments', 'collection', array(
    'type' => new AttachmentType(),
    // 'allow_add' => true,
    // 'allow_delete' => true,
    // 'delete_empty' => true,
));

您还需要为单个附件实体创建AttachmentType表单类型。

收集文档字段类型:http://symfony.com/doc/current/reference/forms/types/collection.html 有关嵌入表单集的更多信息,请参阅:http://symfony.com/doc/current/cookbook/form/form_collections.html

然后还阅读部分:

答案 1 :(得分:1)

好的,那是因为您必须在控制器中初始化TaskType的新实例 - 开头没有分配给此任务的附件。

public function newAction(Request $request)
{
    $task = new Task();

    $attachment1 = new Attachment();
    $task->getAttachments()->add($attachment1);
    $attachment2 = new Attachment();
    $task->getAttachments()->add($attachment2);
    // create form
    $form = $this->createForm(new TaskType(), $task);

    $form->handleRequest($request);
    ...
}

现在应该为新附件提供2个文件输入。

答案 2 :(得分:0)

我添加了一个新的表单类型:AttachmentsType.php

<?php
namespace Seotool\MainBundle\Form\Type;

use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class AttachmentsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('name', 'text');
    $builder->add('file', 'file');
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver
            ->setDefaults(array(
                'data_class' => 'Seotool\MainBundle\Entity\Attachments'
            ));
}

public function getName()
{
    return 'attachments';
}
}

并将其用于将其嵌入TaskType.php

的表单构建器中
$builder->add('attachments', 'collection', array(
    'type' => new AttachmentsType(),
));

但我的输出只给了我以下HTML:

 <div class="form-group"><label class="control-label required">Attachments</label><div id="task_attachments"></div></div><input id="task__token" name="task[_token]" class="form-control" value="brHk4Kk4xyuAhST3TrTHaqwlnA03pbJ5RE4NA0cmY-8" type="hidden"></form>