在Symfony2(Doctrine)中更新具有文件字段的实体

时间:2014-05-09 13:11:17

标签: symfony doctrine

我有一个通过带有文件字段的表单创建的实体。现在,当我想通过相同的表单更新此实体时,我需要重新上载文件。如何在将字段空白处理时预先填充文件字段或不修改这些属性?

编辑:FormType:

class ThemeType extends AbstractType {

    private $edit;
    private $parentFunctionalId;

    public function __construct($parentFunctionalId = null, $edit = false) {
        $this->edit = $edit;
        $this->parentFunctionalId = $parentFunctionalId;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {

        $builder->add('translations', 'collection', array('type' => new ThemeHasTranslationsType(),'allow_add' => true))
            ->add('image1', 'file', array(
                'data_class' => null, 'required' => false
            ))
            ->add('image2', 'file', array(
                'data_class' => null, 'required' => false
            ))
            ->add('style', new StyleType())
            ->add('isVerb', 'checkbox', array('label' => 'Verbe', 'required' => false))
            ->add('isActive', 'checkbox', array('label' => 'Activé', 'required' => false));

            $action = $this->parentFunctionalId != null ? $this->parentFunctionalId : '/app_dev.php/themes';
            $submitLabel = $this->edit ? 'edit' : 'create';
            $builder->setAction($action)
                    ->add($submitLabel, 'submit');

    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'MyApp\BusinessBundle\Entity\Theme'
        ));
    }

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

}

1 个答案:

答案 0 :(得分:0)

您应该使用包含上传文件文件名的第二个属性$image1Filename

这样,您可以在发布时检查表单是否包含文件,并“手动”处理文件上载(最终通过生命周期回调事件)。