模块Prestashop 1.7:始终替换自定义图像上载

模块Prestashop 1.7:始终替换自定义图像上载,prestashop,prestashop-1.7,prestashop-modules,Prestashop,Prestashop 1.7,Prestashop Modules,若我并没有选择我的文件\ url字段,那个么它在数据库中总是会被删除。(即使图像已集成) 如果在这种情况下单击“保存”,则会删除现场PC映像 这是我的AdminCustomController protected function postImage($id) { $file = isset($_FILES['file_url']) ? $_FILES['file_url'] : false; if ($file && is_

若我并没有选择我的文件\ url字段,那个么它在数据库中总是会被删除。(即使图像已集成)

如果在这种情况下单击“保存”,则会删除现场PC映像

这是我的
AdminCustomController

 protected function postImage($id)
    {


        $file = isset($_FILES['file_url']) ? $_FILES['file_url'] : false;



        if ($file && is_uploaded_file($file['tmp_name'])) {


            $path = _PS_MODULE_DIR_ . 'custom/img/';
            $tmp_arr = explode('.', $file['name']);
            $filename = $file['name'];



            if (!Tools::copy($file['tmp_name'], $path . $filename)) {
                $errors[] = Tools::displayError('Failed to load image');
            }

        }
}
这是renderForm()

上传和保存在数据库中是正常的。但是当图像存在时,我不会在字段中选择其他图像。数据库中的
文件\u url
字段被删除

你能帮我吗


谢谢

您需要检查是否上传了图像,然后才更新数据库记录。所以,只要尝试添加
returnfalse
到您的
postImage
方法,并在数据库更新之前进行检查,如

if($this->positmage($id)!==false){
//更新图像记录
}

public function renderForm()
    {
      $image_url = '';

       if($this->object->file_url) {
            $image_url = ImageManager::thumbnail(
                _PS_MODULE_DIR_ . 'homecase/img/' . $this->object->file_url,
                $this->table . $this->object->file_url,
                150,
                'jpg',
                true,
                true
            );
        }
        $this->fields_form = [
            //Entête
            'legend' => [
                'title' => $this->module->l('Edition'),
                'icon' => 'icon-cog'
            ],

                array(
                    'type' => 'file',
                    'label' => $this->l('PC Image'),
                    'name' => 'file_url',
                    'display_image' => true,
                    'image' => $image_url ? $image_url : false,
                ),

....