如何从 opencart 2.3 中的系统购物车库访问位于目录中的自定义模型
How to access my custom model located in catalog from system cart library in opencart 2.3
我正在通过修改将此代码添加到系统购物车库中以访问位于 catalog/model/extension/folder_name/file_name:
中的自定义模型
public function __construct($registry) {
global $loader;
$loader->model('extension/folder_name/file_name');
$this->model = $registry->get('model_extension_folder_name_file_name');
}
但它说:
致命错误:未捕获错误:调用成员函数 model() on null
在 opencart 2.3.0.2
虽然我在 opencart 2.2.0.0 上工作时这段代码没问题。
请帮忙...
你的想法是正确的,只是一个语法错误。
在控制器文件中的 OpenCart 中,通过 $this->load->model()
访问加载器
但是在 system/library/cart/cart.php 你的代码应该是这样的
public function __construct($registry) {
$registry->get('load')->model('extension/folder_name/file_name');
$this->model = $registry->get('model_extension_folder_name_file_name');
}
This is because you are accessing directly in system/library/cart/cart.php, where you have access to the $registry
from the construct but no __get()
and __set()
functions like the controllers and models have.
The cool thing is, the $this->load->model method actually checks where the cart is being called (is it from catalog
folder or admin
folder) and loads accordingly.
You can be safe to load a model in cart.php
because it is only called in catalog
folder, but be careful adding such code to system/library/request.php which is loaded both in catalog
and admin
. this will creat errors.
我正在通过修改将此代码添加到系统购物车库中以访问位于 catalog/model/extension/folder_name/file_name:
中的自定义模型public function __construct($registry) {
global $loader;
$loader->model('extension/folder_name/file_name');
$this->model = $registry->get('model_extension_folder_name_file_name');
}
但它说: 致命错误:未捕获错误:调用成员函数 model() on null 在 opencart 2.3.0.2
虽然我在 opencart 2.2.0.0 上工作时这段代码没问题。
请帮忙...
你的想法是正确的,只是一个语法错误。
在控制器文件中的 OpenCart 中,通过 $this->load->model()
但是在 system/library/cart/cart.php 你的代码应该是这样的
public function __construct($registry) {
$registry->get('load')->model('extension/folder_name/file_name');
$this->model = $registry->get('model_extension_folder_name_file_name');
}
This is because you are accessing directly in system/library/cart/cart.php, where you have access to the
$registry
from the construct but no__get()
and__set()
functions like the controllers and models have.The cool thing is, the $this->load->model method actually checks where the cart is being called (is it from
catalog
folder oradmin
folder) and loads accordingly.You can be safe to load a model in
cart.php
because it is only called incatalog
folder, but be careful adding such code to system/library/request.php which is loaded both incatalog
andadmin
. this will creat errors.