Symfony 2文件上传设置默认文件

时间:2023-01-22 07:07:39

I have a form in my Symfony2 app where I ask a user to upload a file. This field is not required. Currently I process the user`s file with the following command:

我在Symfony2应用程序中有一个表单,我要求用户上传文件。此字段不是必需的。目前,我使用以下命令处理用户的文件:

$file = $form['attachement']->getData();

My question is how can I set a default file in case $file returns NULL, i.e. the user does not want to upload any file? Thank you.

我的问题是,如果$ file返回NULL,我该如何设置默认文件,即用户不想上传任何文件?谢谢。

2 个解决方案

#1


1  

First, you have to create a valid object from your default file.
To do that, use the Symfony\Component\HttpFoundation\File.

首先,您必须从默认文件创建有效对象。为此,请使用Symfony \ Component \ HttpFoundation \ File。

Usage: $file = new File('path/of/your/default/file');

用法:$ file = new文件('path / of / your / default / file');

Then, you have to update your form before the data binding is done.
For that, you have two solutions :

然后,您必须在数据绑定完成之前更新表单。为此,您有两个解决方案:

1- Use a FormEvent to manually set the file if the field is null. (e.g. PRE_SUBMIT and POST_SUBMIT)

1-如果字段为空,请使用FormEvent手动设置文件。 (例如PRE_SUBMIT和POST_SUBMIT)

2- Use a prePersist/preUpdate hook in your entity. (Doesn't involves to move your logic, the unmapped property 'attachment' should already exists in your entity)

2-在实体中使用prePersist / preUpdate挂钩。 (不涉及移动您的逻辑,未映射的属性'attachment'应该已经存在于您的实体中)

For the option 1, see the Dynamic form modification chapter of the cookbook and look for the appropriated event (the documentation will give the informations to choose it).

对于选项1,请参阅cookbook的动态表单修改章节并查找适当的事件(文档将提供选择它的信息)。

For the 2, see the Events chapterof Doctrine documentation.
Something like the following should do the job :

对于2,请参阅Doctrine文档的Events章节。像下面这样的东西应该做的工作:

/**
 * @ORM\HasLifecycleCallbacks
 */
class YourEntity {
    /**
     *
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function manageFile()
    {
        if ($this->attachment === null) {
            $file = new File('path/of/your/default/file');
            $this->setAttachment($file);
        }
    }
}

I hope it's sufficient for you.

我希望这对你来说已经足够了。

#2


0  

I guess, doctrine Lifecycle Events should help you. Use prePersist or preUpdate event to check is $this->file === null, and set default value.

我想,doctrine Lifecycle Events应该可以帮到你。使用prePersist或preUpdate事件检查$ this-> file === null,并设置默认值。

#1


1  

First, you have to create a valid object from your default file.
To do that, use the Symfony\Component\HttpFoundation\File.

首先,您必须从默认文件创建有效对象。为此,请使用Symfony \ Component \ HttpFoundation \ File。

Usage: $file = new File('path/of/your/default/file');

用法:$ file = new文件('path / of / your / default / file');

Then, you have to update your form before the data binding is done.
For that, you have two solutions :

然后,您必须在数据绑定完成之前更新表单。为此,您有两个解决方案:

1- Use a FormEvent to manually set the file if the field is null. (e.g. PRE_SUBMIT and POST_SUBMIT)

1-如果字段为空,请使用FormEvent手动设置文件。 (例如PRE_SUBMIT和POST_SUBMIT)

2- Use a prePersist/preUpdate hook in your entity. (Doesn't involves to move your logic, the unmapped property 'attachment' should already exists in your entity)

2-在实体中使用prePersist / preUpdate挂钩。 (不涉及移动您的逻辑,未映射的属性'attachment'应该已经存在于您的实体中)

For the option 1, see the Dynamic form modification chapter of the cookbook and look for the appropriated event (the documentation will give the informations to choose it).

对于选项1,请参阅cookbook的动态表单修改章节并查找适当的事件(文档将提供选择它的信息)。

For the 2, see the Events chapterof Doctrine documentation.
Something like the following should do the job :

对于2,请参阅Doctrine文档的Events章节。像下面这样的东西应该做的工作:

/**
 * @ORM\HasLifecycleCallbacks
 */
class YourEntity {
    /**
     *
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function manageFile()
    {
        if ($this->attachment === null) {
            $file = new File('path/of/your/default/file');
            $this->setAttachment($file);
        }
    }
}

I hope it's sufficient for you.

我希望这对你来说已经足够了。

#2


0  

I guess, doctrine Lifecycle Events should help you. Use prePersist or preUpdate event to check is $this->file === null, and set default value.

我想,doctrine Lifecycle Events应该可以帮到你。使用prePersist或preUpdate事件检查$ this-> file === null,并设置默认值。