OpenCart load Model outside Controller

你说的曾经没有我的故事 提交于 2019-12-03 03:32:38

If it's only one method that you need to copy, you would be best adding a method to the Cart class itself. The Cart class will work with the $this->db->query() calls as it already has $db assigned to it even though it's not a Controller/Model

Edit

Should you wish to do this, you could do something similar to the following

public function test() {
    global $loader, $registry;
    $loader->model('catalog/product');
    $model = $registry->get('model_catalog_product');
    $result = $model->getProduct(123);
}

You are able to load a model outside a controlled.

If You need to load a model inside of another model, You could load it the very same way using $this->load->model('my_module/my_model');.

If You need to load a model inside of template file or other custom PHP script, look at the index.php file where the Registry is instanciated - You would need to instanciate it the same way. So Your custom code could look like:

$registry = new Registry();
$my_model = $registry->load->model('my_module/my_model');
$my_model->customFunction();

Anyway I highly recommend not to edit/change the core library files unless You are sure there is no other way how to implement/do what You need.

As Jay Gilford proposed I would implement that function or it's call to the catalog/checkout/cart.php, potentially confirm or success depending on the scope and functionality You would like to implement.

In OpenCart parts of the model layer are dependent on the libraries. One example is that the ID of the current customer (frontend) or the current user (backend) is needed.

To keep the structure clear and clean and to reduce code duplication, you should not introduce a dependency in the other direction. If a model is dependent on the class user and the class user is dependent on the model, you created a vicious circle.

I would recommend to move the functions into the library layer and remove their implementations in the model layer. To minimize the impact, you can wrap the function call within the model.

library/myClass

function getValue($id){ 
 $sql = "select ..."; 
 $query = $this->db->query($sql); 
 return $query->rows;
}

admin/model/catalog/myModel

function getValue($id){
 return $this->myClass->getValue();
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!