我目前正在开发一个 PHP v5.5.9 项目,其中我使用了一些独立组件(我不使用全栈框架),目前如下:
psr/log 1.0.0 Common interface for logging libraries
sonata-project/intl-bundle 2.2.1 Symfony SonataIntlBundle
symfony/debug v2.4.2 Symfony Debug Component
symfony/dependency-injection v2.4.2 Symfony DependencyInjection Component
symfony/event-dispatcher v2.4.2 Symfony EventDispatcher Component
symfony/form v2.4.2 Symfony Form Component
symfony/http-foundation v2.4.2 Symfony HttpFoundation Component
symfony/http-kernel v2.4.2 Symfony HttpKernel Component
symfony/icu v1.2.0 Contains an excerpt of the ICU data and classes to load it.
symfony/intl v2.4.2 A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/locale v2.4.2 Symfony Locale Component
symfony/options-resolver v2.4.2 Symfony OptionsResolver Component
symfony/property-access v2.4.2 Symfony PropertyAccess Component
symfony/security-core v2.4.2 Symfony Security Component - Core Library
symfony/security-csrf v2.4.2 Symfony Security Component - CSRF Library
symfony/templating v2.4.2 Symfony Templating Component
symfony/translation v2.4.2 Symfony Translation Component
symfony/twig-bridge v2.4.2 Symfony Twig Bridge
symfony/validator v2.4.2 Symfony Validator Component
twig/extensions v1.0.1 Common additional features for Twig that do not directly belong in core
twig/twig v1.15.1 Twig, the flexible, fast, and secure template language for PHP
我有一个Entity类、一个FormType类和一个Controller,我可以通过“Twig”模板View来持久化Entity。所以整个堆栈工作正常。
然后我向我的EntityFile
添加了一个属性,以便按照官方文档中的描述添加图像文件] 1。我还使用以下代码扩展了我的FormType :
$builder->add('pictureFile', 'file', array('label' => 'Image File'));
运行更新后的代码(选择图像文件)后,显示以下错误:
Catchable fatal error: Argument 1 passed to MyEntity::setPictureFile() must be an instance of Symfony\Component\HttpFoundation\File\File, array given, called in [.]\vendor\symfony\property-access\Symfony\Component\PropertyAccess\PropertyAccessor.php on line [.] and defined in MyEntity.php on line [.]
因此,Symfony不会自动从表单创建一个UploadedFile
,而是将数据绑定$_FILE
到一个array
. 我注意到,其他人也有这个问题:
目前我在Controller中使用以下代码,但我想使用自动绑定功能(因为目前我无法在Entity中验证上传的文件)。
$formType = new MyType;
$entity = new MyEntity;
$form = $this->formFactory->create($formType, $entity);
$form->handleRequest();
if (true === $form->isSubmitted()) {
if (true === $form->isValid()) {
// TODO Replace ugly workaround to partially solve the problem of the file upload.
$request = Request::createFromGlobals();
$file = $request->files->get('myentity')['pictureFile'];
// Persist entity, upload file to server and redirect.
}
}
// Load View template.
编辑(2014-03-03):这是实体的相关代码:
<?php
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;
class MyEntity
{
private $id;
private $name;
private $picture;
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function getPictureFile()
{
return $this->pictureFile;
}
public function setPictureFile(File $pictureFile = null)
{
$this->pictureFile = $pictureFile;
}
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint(
'name',
new Assert\NotBlank
);
$metadata->addPropertyConstraint(
'name',
new Assert\Type('string'));
$metadata->addPropertyConstraint(
'pictureFile',
new Assert\Image(
array(
'maxSize' => '2048k',
'minWidth' => 640,
'maxWidth' => 4096,
'minHeight' => 480,
'maxHeight' => 4096
)
)
);
}
}
以及FormType的源代码:
<?php
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class MyType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text')
->add(
'pictureFile',
'file',
array('label' => 'Image File')
)
->add('submit', 'submit', array('label' => 'Save'));
}
public function getName()
{
return 'myentity';
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array('data_class' => __NAMESPACE__ . '\MyEntity'));
}
}
有什么建议如何完成这项工作吗?
编辑(2014-03-07):简化源代码以准备我的回答。