使用 1..1 或 n..1 关系可上传的 Symfony2 stof 学说

Symfony2 stof doctrine uploadable with 1..1 or n..1 relations

我在使用 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,
        ))
    ;
}

当我提交时,它告诉我所有实体字段(mimetype、大小、名称)都不能为空。 但通常它们都充满了扩展名(如第一种情况)

我该如何管理?

谢谢

我认为这个名字没有被处理。检查文档,每个演示表格中都会询问名称。

我想我解决了我的问题,

这是我所做的:

    $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();
        }
    }