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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With