【问题标题】:How to get supplier id when it has just been added in the db?刚刚添加到数据库中时如何获取供应商ID?
【发布时间】:2015-12-19 14:13:16
【问题描述】:

当我在管理面板中添加供应商(并单击保存按钮)时,我想在控制器中的 postProcess() 方法中检索其 ID

prestashop/controllers/admin/AdminSuppliersController.php

以这样一种方式,我可以将数据库中自定义表中的其他自定义信息与此供应商相关联。将供应商存储到数据库时,我无法在代码中找到该部分(当它在 ps_address 表中插入相对于供应商的地址时,我只找到该部分)。

这里默认的postProcess()方法:

public function postProcess()
{
    // checks access
    if (Tools::isSubmit('submitAdd'.$this->table) && !($this->tabAccess['add'] === '1')) {
        $this->errors[] = Tools::displayError('You do not have permission to add suppliers.');
        return parent::postProcess();
    }

    if (Tools::isSubmit('submitAdd'.$this->table)) {
        if (Tools::isSubmit('id_supplier') && !($obj = $this->loadObject(true))) {
            return;
        }

        // updates/creates address if it does not exist
        if (Tools::isSubmit('id_address') && (int)Tools::getValue('id_address') > 0) {
            $address = new Address((int)Tools::getValue('id_address'));
        } // updates address
        else {
            $address = new Address();
        } // creates address

        $address->alias = Tools::getValue('name', null);
        $address->lastname = 'supplier'; // skip problem with numeric characters in supplier name
        $address->firstname = 'supplier'; // skip problem with numeric characters in supplier name
        $address->address1 = Tools::getValue('address', null);
        $address->address2 = Tools::getValue('address2', null);
        $address->postcode = Tools::getValue('postcode', null);
        $address->phone = Tools::getValue('phone', null);
        $address->phone_mobile = Tools::getValue('phone_mobile', null);
        $address->id_country = Tools::getValue('id_country', null);
        $address->id_state = Tools::getValue('id_state', null);
        $address->city = Tools::getValue('city', null);

        $validation = $address->validateController();

        // checks address validity
        if (count($validation) > 0) {
            foreach ($validation as $item) {
                $this->errors[] = $item;
            }
            $this->errors[] = Tools::displayError('The address is not correct. Please make sure all of the required fields are completed.');
        } else {
            if (Tools::isSubmit('id_address') && Tools::getValue('id_address') > 0) {
                $address->update();
            } else {
                $address->save();
                // here I want to get the ID of the inserted supplier
                $_POST['id_address'] = $address->id;
            }
        }
        return parent::postProcess();
    } elseif (Tools::isSubmit('delete'.$this->table)) {
        if (!($obj = $this->loadObject(true))) {
            return;
        } elseif (SupplyOrder::supplierHasPendingOrders($obj->id)) {
            $this->errors[] = $this->l('It is not possible to delete a supplier if there are pending supplier orders.');
        } else {
            //delete all product_supplier linked to this supplier
            Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'product_supplier` WHERE `id_supplier`='.(int)$obj->id);

            $id_address = Address::getAddressIdBySupplierId($obj->id);
            $address = new Address($id_address);
            if (Validate::isLoadedObject($address)) {
                $address->deleted = 1;
                $address->save();
            }
            return parent::postProcess();
        }
    } else {
        return parent::postProcess();
    }
}

【问题讨论】:

    标签: php content-management-system prestashop prestashop-1.6


    【解决方案1】:

    您可以使用钩子actionObjectSupplierAddAfter 在将供应商对象添加到数据库后立即在您的模块中使用类似:

    public function hookActionObjectSupplierAddAfter($params) {
        $supplier = $params['object'];
    }
    

    【讨论】:

    • 感谢您的回答,但您能更具体一点吗?我在这里是菜鸟......我必须在哪里插入(然后调用)这个函数?
    • 该函数将进入您创建的自定义模块,在此处了解更多信息doc.prestashop.com/display/PS16/Creating+a+first+module。为确保调用该函数,请使用 $this->registerHook('actionObjectSupplierAddAfter')install() 函数中注册钩子上的模块@
    • 这非常有用,但我无法在这个钩子中获取对象 ID...还有其他想法吗?
    猜你喜欢
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多