在opencart中加载和使用模型

Cod*_*ack 4 codeigniter opencart

根据我的理解,Open Cart基于CodeIgniter但在CodeIgniter中加载和使用模型你会做这样的事情

$this->load->model('Model_name');

$this->Model_name->function();
Run Code Online (Sandbox Code Playgroud)

在OpenCart中你可以做这样的事情

$this->load->model('catalog/product');
$this->model_catalog_product->getTotalProducts()
Run Code Online (Sandbox Code Playgroud)

这是如何工作的以及"model_catalog_product"的来源?

除了论坛之外,他们似乎还有0个开发者文档.

Max*_*rin 6

OpenCart的加载器类似乎受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();                 
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以清楚地看到它用下划线替换斜杠并'model_'在模型名称前附加.这就是你最终的原因model_catalog_product.