控制器外部的OpenCart负载模型 [英] OpenCart load Model outside Controller

查看:92
本文介绍了控制器外部的OpenCart负载模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个OpenCart项目,该项目需要大量的自定义. 对于我的项目,我必须在购物车库(system/library/cart.php)中进行一些更改.

I'm working on an OpenCart project, that requires a lot of customization. for my project I have to change something in the cart library (system/library/cart.php).

我必须调用在产品模型(catalog/model/catalog/product.php)中定义的自定义函数.

I would have to call a custom function that's defined inside the product model (catalog/model/catalog/product.php).

在控制器中,加载模型并使用其功能很容易:

In a controller, loading a Model and using its functions is easy:

    $this->load->model("catalog/product");
    $this->model_catalog_product->customFunction();

但是如何在控制器外部加载模型? 您无法创建模型的新实例,我已经尝试过:

But how do you load a model outside a controller? You can't create a new instance of the model, I already tried that:

    require_once("catalog/model/catalog/product.php");
    $a_model = new ModelCatalogProduct();

这显然不起作用,因为不打算以这种方式使用模型.

This obviously doesn't work cause models weren't intended to be used in such a way.

我也尝试使用范围解析运算符(ModelCatalogProduct :: customFunction()) 也不行.

I also tried to use the scope resolution operator ( ModelCatalogProduct::customFunction()) It doesn't work either.

我可以将所有必需的信息作为参数传递,但是我宁愿在购物车库类中使用该模型,因为更改将是全局的.

I could pass all the required info as arguments, but I would rather use the model inside the cart library class, cause the changes would be global.

是否甚至可以在OpenCart的控制器外部加载模型?

Is it even possible to load a model outside a controller in OpenCart?

推荐答案

如果仅是一种需要复制的方法,则最好将方法添加到Cart类本身. Cart类将与$this->db->query()调用一起使用,因为它已经分配了$db,即使它不是控制器/模型

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

如果您希望这样做,可以执行以下类似的操作

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);
}

这篇关于控制器外部的OpenCart负载模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆