OpenCart中的致命错误:调用未定义的函数错误


fatal error in OpenCart: call to undefined function error

我不想问一个已经得到回答的问题,但我已经做了大量的研究,我被卡住了。如有任何帮助,我们将不胜感激。

我正在尝试查询数据库结果并将其添加到OpenCart的addproduct.tpl 中

MODEL文件中,我有:

public function units() { //function that gets database info and returns it
$unit_variables = $this->db->query("SELECT unit FROM  ". DB_PREFIX ."
weight_class_description");
if (!$unit_variables) {
die(mysql_error());
}
else {
foreach ($unit_variables->rows as $temp_var) {
print_r($temp_var['unit'], true);
}   
}   
}

CONTROLLER文件中,我有:

$this->load->model('catalog/product'); //where my function is located
$this->model_catalog_product->units();
$weight_variables = units(); 
if (isset($this->request->post['weight_variables'])){
$this->data['weight_variables'] = $this->request->post['weight_variables'];
}

视图中,我有:

<?php echo $weight_variables ?>

我得到以下错误:

Call to undefined function units() in /path/to/controller/file on line etc.

注意:当我print_r($temp_var);而不是返回print_R($temp_var, true)并删除控制器文件中的这些代码行$weight_variables = units(); if (isset($this->request->post['weight_variables'])){ $this->data['weight_variables'] = $this->request->post['weight_variables'] }时,我的模型文件将在addproduct.tpl 上显示查询结果

units()是对象的METHOD,但您将其作为一个独立的正则函数调用:

$weight_variables = units();
                    ^^^^^^^

不是吗:

$weight_variables = $this->model_catalog_product->units();

相反?注意,正如所写的,您的方法实际上没有返回任何内容,所以$weight_variables只会被分配给null。。