如何通过控制器定义MySQL(模型)值

问题描述 投票:0回答:1

我正在尝试通过控制器定义MySQL(model.php)值

这是MySql的代码

$this->db->query("INSERT INTO " . DB_PREFIX . "cart_to_customer SET customer_id = '" . (int)$this->customer->getId() . "', cart_id = '" . (int)$data['cart_id'] . "'");

我只想通过controller.php插入cart_id值

例如

$data['cart_id'] = '45';

我尝试使用view.tpl的输入尝试以下内容,并且工作正常,但是当存在null值时,它必须在其中插入45,但给出0。

if (isset($this->request->post['cart_id'])) {
            $data['cart_id'] = $this->request->post['cart_id'];
        } else {
            $data['cart_id'] = '45';
        }

我如何从控制器设置值?

php mysql mysqli opencart
1个回答
0
投票

请指导如何...假设您有控制器文件catalog/controller/extension/module/your_estension.php和与此控制器文件相对应的模型文件。.catalog/model/extension/module/your_estension.php。在您的控制器文件中:

// you must load model file like this:

    $this->load->model('extension/module/your_estension');

    if (isset($this->request->post['cart_id'])) {
                $data['cart_id'] = $this->request->post['cart_id'];
            } else {
                $data['cart_id'] = '45';
            }
    //next you can send your posted data to the model...
    $this->model_extension_module_your_estension->addCardId($data['cart_id']);

    // next in the corresponding model file you can retrieve that data:

    public function addCardId($card_id) {
    // and write it to DB
    $this->db->query("INSERT INTO " . DB_PREFIX . "cart_to_customer SET customer_id = '" . (int)$this->customer->getId() . "', cart_id = '" . (int)$card_id . "'");
    }
© www.soinside.com 2019 - 2024. All rights reserved.