Download and use models in opencart

The open basket is based on CodeIgniter, as I understand it, but in CodeIgniter you do something like this to load and use the model

$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 it work and where does "model_catalog_product" come from?

It looks like they have 0 documentation for developers other than their forums.

+4
source share
2 answers

The OpenCart bootloader class seems inspired by CodeIgniter, but it is not based on it. You can look at 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 appends 'model_' in front of the model name. This is why you end up with model_catalog_product .

+6
source

model_catalog_product comes from the folder structure of the path and the file name in the model folder, so model_catalog_product is a model/catalog/product.php file with the extension removed, and the slashes are changed to underscores. Also note that the model class name also refers to a similar structure, which is equal to ModelCatalogProduct . Regarding the documentation, there was some kind of documentation for the developers, but it was just checked and it seems that it was deleted for some reason. Unfortunately, I learned from many trial and error, as most developers use it

+2
source

Source: https://habr.com/ru/post/1447102/


All Articles