Forms Prestashop助手表单';文件';类型

Forms Prestashop助手表单';文件';类型,forms,file-upload,prestashop,helper,Forms,File Upload,Prestashop,Helper,我在prestasshop上工作,我在控制器内创建了一个助手表单(用于后台)。我的问题是如何使用helper表单中的“file”类型上传文档。代码如下: public function __construct() { $this->context = Context::getContext(); $this->table = 'games'; $this->className = 'Games'; $this->lang = true;

我在prestasshop上工作,我在控制器内创建了一个助手表单(用于后台)。我的问题是如何使用helper表单中的“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();
}
public function\uuuu construct()
{
$this->context=context::getContext();
$this->table='games';
$this->className='Games';
$this->lang=true;
$this->addRowAction('edit');
$this->addRowAction('delete');
$this->bulk\u actions=array('delete'=>array('text'=>$this->l('delete selected'),
'确认'=>$this->l('删除所选项目?');
$this->multishop\u context=Shop::context\u ALL;
$this->fieldImageSettings=array(
“名称”=>“图像”,
'dir'=>'游戏'
);
$this->fields\u list=数组(
'id_game'=>数组(
'title'=>this->l('ID'),
“宽度”=>25
)
);
$this->identifier='id_game';
父项::_构造();
}
公共函数renderForm()
{
如果(!($obj=$this->loadObject(true)))
返回;
$games_list=Activity::getGamesList();
$this->fields\u form=array(
“tinymce”=>正确,
“图例”=>数组(
'title'=>this->l('Game'),
'image'=>'../img/admin/tab payment.gif'
),
'输入'=>数组(
排列(
'类型'=>'选择',
'label'=>$this->l('Game:'),
'desc'=>this->l('选择游戏'),
'name'=>'id_games',
“必需”=>true,
“选项”=>数组(
“查询”=>$games\u列表,
'id'=>'id_game',
“名称”=>“名称”
)
),
排列(
'类型'=>'文本',
'label'=>$this->l('游戏名称:'),
“名称”=>“名称”,
“大小”=>64,
“必需”=>true,
“lang”=>对,
'hint'=>$this->l('无效字符:')。;={}
),
排列(
'类型'=>'文件',
'label'=>this->l('Photo:'),
'name'=>'uploadedfile',
'id'=>'uploadedfile',
“显示图像”=>false,
“必需”=>false,
'desc'=>$this->l('上传您的文档')
)
)
);
$this->fields\u form['submit']=array(
'title'=>$this->l('Save'),
“类”=>“按钮”
);
返回AdminController::renderForm();
}
现在我如何上传文件? 我是否必须在db(游戏表)的表中创建一列来存储文件或相关内容


提前谢谢

我想这款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

如果允许更新文件字段,则还需要执行以下步骤来更新方法。

我假定此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

如果允许更新文件字段,则还需要执行以下步骤来更新方法。

我假定此AdminController适用于您的模型。现在,模型显然不能在表列中保存文件。您可以做的是保留上传文件的路径。这就是你能挽救的

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

processAdd()
processUpdate()
现在研究这些方法中的流逻辑。从该方法中调用其他方法,例如:

$this->beforeAdd($this->object); -> calls $this->_childValidation();
$this->validateRules();
$this->afterUpdate($object);
正如你所看到的,这里有一些方法,你可以做你自己的事情。如果您在AdminController类中查找这些函数,则该函数为空。它们是特意添加的,因此人们可以覆盖它们并将其自定义逻辑放在那里

因此,使用这些函数,您可以验证上载的文件字段(即使它不在模型本身中),如果它验证了,那么您可以为对象分配路径;然后在beforeAdd方法中,您可以实际移动