Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading and using models in opencart

Open cart is based on CodeIgniter as I understand but in CodeIgniter to load and use the model you do something like this

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

$this->Model_name->function();

In OpenCart you do something like this

$this->load->model('catalog/product');
$this->model_catalog_product->getTotalProducts()

How does this work and where does the "model_catalog_product" come from?

It seems like they have 0 developer documentation besides their forums.

like image 671
CodeCrack Avatar asked Feb 19 '23 10:02

CodeCrack


1 Answers

OpenCart's loader class seems to be inspired by CodeIgniter, but it's not based on it. You can look into the source of OpenCart, see file system/engine/loader.php (Line 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();                 
    }
}

You can clearly see that it replaces slashes with underscores and append 'model_' before the model's name. That's why you end up with model_catalog_product.

like image 190
Maxime Morin Avatar answered Feb 27 '23 07:02

Maxime Morin