【问题标题】:Prestashop helper form 'file' typePrestashop 助手表单“文件”类型
【发布时间】:2015-01-28 02:39:44
【问题描述】:

我在 prestasshop 上工作,我在控制器内创建了一个辅助表单(用于后台)。我的问题是如何使用帮助表单中的 type:'file' 上传文档。代码如下:

public function __construct()
{
    $this->context = Context::getContext();
    $this->table = 'games';
    $this->className = 'Games';
    $this->lang = true;
    $this->addRowAction('edit');
    $this->addRowAction('delete');
    $this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'),
    'confirm' => $this->l('Delete selected items?')));
    $this->multishop_context = Shop::CONTEXT_ALL;

    $this->fieldImageSettings = array(
        'name' => 'image',
        'dir' => 'games'
    );

    $this->fields_list = array(
        'id_game' => array(
            'title' => $this->l('ID'),
            'width' => 25
        )
    );

    $this->identifier = 'id_game';
    parent::__construct();
}

public function renderForm()
{
    if (!($obj = $this->loadObject(true)))
        return;

    $games_list = Activity::getGamesList();     

    $this->fields_form = array(
        'tinymce' => true,          
        'legend' => array(
            'title' => $this->l('Game'),
            'image' => '../img/admin/tab-payment.gif'
        ),
        'input' => array(
            array(
                  'type' => 'select',
                  'label' => $this->l('Game:'),
                  'desc' => $this->l('Choose a Game'),
                  'name' => 'id_games',
                  'required' => true,
                  'options' => array(
                        'query' => $games_list,
                        'id' => 'id_game',
                        'name' => 'name'
                    )
            ),
            array(
                'type' => 'text',
                'label' => $this->l('Game Title:'),
                'name' => 'name',
                'size' => 64,
                'required' => true,
                'lang' => true,
                'hint' => $this->l('Invalid characters:').' <>;=#{}'
            ),
            array(
                'type' => 'file',
                'label' => $this->l('Photo:'),
                'name' => 'uploadedfile',
                'id' => 'uploadedfile',
                'display_image' => false,
                'required' => false,
                'desc' => $this->l('Upload your document')
            )
        )
    );

    $this->fields_form['submit'] = array(
        'title' => $this->l('   Save   '),
        'class' => 'button'
    );

    return AdminController::renderForm();
}

现在如何上传文件? 我是否必须在 db(游戏表)的表中创建一列来存储文件或相关内容?

提前致谢

【问题讨论】:

    标签: forms file-upload prestashop helper


    【解决方案1】:

    我为您的模型假设这个 AdminController。现在模型显然不能在表列中保存文件。您可以做的是保存上传文件的路径。这就是你可以保存的。

    您应该查看 AdminController 类(您扩展的)。当您提交表单时,会执行以下两种方法之一:

    processAdd()
    processUpdate()
    

    现在研究这些方法中的流程逻辑。在此方法中调用其他方法,例如:

    $this->beforeAdd($this->object); -> calls $this->_childValidation();
    $this->validateRules();
    $this->afterUpdate($object);
    

    如您所见,您可以通过这些方法进行自定义操作。如果你在 AdminController 类中查找这些函数,它们是空的。它们是故意添加的,因此人们可以覆盖它们并将他们的自定义逻辑放在那里。

    因此,使用这些函数,您可以验证您上传的文件字段(即使它不在模型本身中),如果它验证您可以为对象分配路径;然后在 beforeAdd 方法中,您实际上可以将上传的文件移动到所需的位置(因为子验证和默认验证都已通过)。

    我的做法:

    protected function _childValidation()
    {
       // Check upload errors, file type, writing permissions
       // Use $this->errors['file'] if there is an error;
    
    protected function beforeAdd($object)
    {
       // create filename and filepath
       // assign these fields to object;
    
    protected function afterAdd($object)
    {
       // move the file
    

    如果您允许更新文件字段,您还需要执行更新方法的这些步骤。

    【讨论】:

    • 感谢您的有用回复!在这种情况下,我应该在哪里编写实际上传的实际代码?类似于 ((move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)))。
    【解决方案2】:

    你可以在函数 processAdd() 和 processUpdate() 中使用 $_FILES['uploadedfile'] 来获取上传的文件,你可以在调用 $this->object->save() 之前检查那里的所有条件;要保存表单数据,您可以编写代码以将文件上传到所需位置,例如

    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)

    由于您无法将文件保存在数据库中,因此您只需保存文件名或数据库中的位置

    希望有帮助

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 2020-04-14
      • 2011-09-27
      • 1970-01-01
      • 2015-03-19
      相关资源
      最近更新 更多