Symfony2 stof doctrine可以上传1..1或n..1关系

时间:2015-01-26 09:46:12

标签: symfony upload one-to-one stofdoctrineextensions

我无法使用stofdoctrinebundle

的可上传扩展程序

我是一个文件实体:

<?php

namespace my\TestBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
 * File
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="my\TestBundle\Entity\FileRepository")
 * @Gedmo\Uploadable(path="uploads", filenameGenerator="SHA1", allowOverwrite=true, appendNumber=true)
 */
class File
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @ORM\Column(name="path", type="string")
     * @Gedmo\UploadableFilePath
     */
    private $path;

    /**
     * @ORM\Column(name="name", type="string")
     * @Gedmo\UploadableFileName
     */
    private $name;

    /**
     * @ORM\Column(name="mime_type", type="string")
     * @Gedmo\UploadableFileMimeType
     */
    private $mimeType;

    /**
     * @ORM\Column(name="size", type="decimal")
     * @Gedmo\UploadableFileSize
     */
    private $size;

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
    return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return File
     */
    public function setName($name)
    {
    $this->name = $name;

    return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
    return $this->name;
    }

    /**
     * Set path
     *
     * @param string $path
     * @return File
     */
    public function setPath($path)
    {
    $this->path = $path;

    return $this;
    }

    /**
     * Get path
     *
     * @return string 
     */
    public function getPath()
    {
    return $this->path;
    }

    /**
     * Set mimeType
     *
     * @param string $mimeType
     * @return File
     */
    public function setMimeType($mimeType)
    {
    $this->mimeType = $mimeType;

    return $this;
    }

    /**
     * Get mimeType
     *
     * @return string 
     */
    public function getMimeType()
    {
    return $this->mimeType;
    }

    /**
     * Set size
     *
     * @param string $size
     * @return File
     */
    public function setSize($size)
    {
    $this->size = $size;

    return $this;
    }

    /**
     * Get size
     *
     * @return string 
     */
    public function getSize()
    {
    return $this->size;
    }
}

在我的控制器中,当我直接在此实体上使用表单时:

$document = new File();
$form = $this->createFormBuilder($document)
        ->add('name')
        ->add('path','file',array(
                    'data_class' => null ))
        ->add('submit','submit')
        ->getForm()

if ($this->getRequest()->getMethod() === 'POST') {

    $form->handleRequest($this->getRequest());


    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();

        $em->persist($club);


        $uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');

        $uploadableManager->markEntityToUpload($club, $club->getLogo()->getPath());

        $em->flush();


    }
}

我的文件上传得很好,我的实体正确填写

但我想在另一个实体中使用它:

class Company {
/**
 * @ORM\ManyToOne(targetEntity="my\TestBundle\Entity\File", cascade={"persist"})
 * @ORM\JoinColumn(name="logo", nullable=true)
 */
 private $logo;


/**
* Get logo
*
* @return \my\TestBundle\Entity\File 
*/
public function getLogo()
{
    return $this->logo;
}

/**
* Set comments
*
* @param string $comments
* @return Club
*/
public function setComments($comments)
{
    $this->comments = $comments;

    return $this;
}

在我的控制器中:

$company = new Company();
    $form = $this->createFormBuilder($company)
        ->add('name')
        ->add('logo', new \my\TestBundle\Form\FileType, array(
                    'data_class' => 'my\TestBundle\Entity\File' ))
        ->add('submit','submit')
        ->getForm()
    ;


    if ($this->getRequest()->getMethod() === 'POST') {

        $form->handleRequest($this->getRequest());


        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();

            $em->persist($club);


            $uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');

            $uploadableManager->markEntityToUpload($company, $company->getLogo()->getPath());

            $em->flush();


        }
    }

我的文件类型:

/**
 *
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('path', 'file', array(
            'required' => false,
        ))
    ;
}

当我提交时,它告诉我所有dile实体字段(mimetype,size,name)不能为null。 但通常情况下,他们会填充扩展名(如第一种情况)

我该如何管理?

由于

2 个答案:

答案 0 :(得分:1)

我认为我解决了我的问题,

这里我做了什么:

    $company = new Company();
    $form = $this->createFormBuilder($company)
        ->add('name')
        ->add('logo', new \cM\ManagementBundle\Form\FileType, array(
                    'data_class' => 'cM\ManagementBundle\Entity\File' ))
        ->add('submit','submit')
        ->getForm()
    ;

    if ($this->getRequest()->getMethod() === 'POST') {
        $form->handleRequest($this->getRequest());
        if ($form->isValid()) {                
            $em = $this->getDoctrine()->getManager();    
            $em->persist($company);

            $uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');
            $uploadableManager->markEntityToUpload($club->getLogo(), $club->getLogo()->getPath());

            $em->flush();
        }
    }

答案 1 :(得分:0)

我不认为这个名字得到了处理。检查文档,在每个演示表单中询问姓名。