如何通过 OpenCart 中的控制器定义 MySQL(model) 值

How to define MySQL(model) value through controller in OpenCart

我正在尝试通过控制器定义 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 的输入尝试了下面的操作并且工作正常但是当有空值时它必须插入 45 但它给出 0.

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

如何从控制器设置值??

指导如何... 假设您有控制器文件 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 . "'");
    }