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、サイズ、名前) のすべてのフィールドを null にすることはできないことがわかります。しかし、通常、それらは拡張子で満たされています(最初のケースのように)
どうすればこれを管理できますか?
ありがとう