据我所知,开放式购物车基于 CodeIgniter,但在 CodeIgniter 中加载和使用模型,您可以执行以下操作
$this->load->model('Model_name');
$this->Model_name->function();
在 OpenCart 你做这样的事情
$this->load->model('catalog/product');
$this->model_catalog_product->getTotalProducts()
这是如何工作的,“model_catalog_product”从哪里来?
除了论坛之外,他们似乎还有 0 个开发人员文档。
最佳答案
OpenCart 的加载器类 seems to be inspired由 CodeIgniter 编写,但并非基于它。您可以查看 OpenCart 的源代码,参见文件 system/engine/loader.php
(第 39 行)。
public function model($model) {
$file = DIR_APPLICATION . 'model/' . $model . '.php';
$class = 'Model' . preg_replace('/[^a-zA-Z0-9]/', '', $model);
if (file_exists($file)) {
include_once($file);
// Right here. Replaces slash by underscore.
$this->registry->set('model_' . str_replace('/', '_', $model), new $class($this->registry));
} else {
trigger_error('Error: Could not load model ' . $model . '!');
exit();
}
}
您可以清楚地看到它用下划线替换斜线并附加
'model_'
在模型名称之前。这就是为什么你最终会得到 model_catalog_product
.
关于codeigniter - 在 opencart 中加载和使用模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13464415/