如何在PHP中修复以下错误?

时间:2016-05-29 09:57:55

标签: php error-handling

我有两段区域和类别的代码。它们完全一样。类别的代码有效,但“Region”会返回以下错误:

Fatal error: Call to a member function getRegion() on null in C:\wamp64\www\site\catalog\controller\module\latest.php on line 81

// Region

$product_region = array();
$regions = $this->model_profile_profile->getProductRegions($result['product_id']);
foreach ($regions as $region_id) {

print_r($region_id); // !!! ($region_id = 2) !!! $region_id is not empty !!!
    $region_info = $this->model_account_region->getRegion($region_id); // LINE 81

    if ($region_info) {
        $product_region[] = array(
            'name' => ($region_info['path']) ? $region_info['path'] . ' > ' . $region_info['name'] : $category_info['name'],
            'href'  => $this->url->link('account/profile', '&path=' . $region_info['region_id'])
        );
    }
}

以下是可能导致错误的代码:

public function getRegion($region_id) {
    $query = $this->db->query("SELECT DISTINCT *, (SELECT GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR '  >  ') FROM " . DB_PREFIX . "region_path cp LEFT JOIN " . DB_PREFIX . "region_description cd1 ON (cp.path_id = cd1.region_id AND cp.region_id != cp.path_id) WHERE cp.region_id = c.region_id AND cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' GROUP BY cp.region_id) AS path, (SELECT DISTINCT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'region_id=" . (int)$region_id . "') AS keyword FROM " . DB_PREFIX . "region c LEFT JOIN " . DB_PREFIX . "region_description cd2 ON (c.region_id = cd2.region_id) WHERE c.region_id = '" . (int)$region_id . "' AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'");

    return $query->row;
}

如果你通过提供一段代码帮助我,我将不胜感激,因为我在PHP中是假的。

2 个答案:

答案 0 :(得分:2)

$this->model_account_region  //This property is null

检查

echo 'FILE : '.__FILE__ .'<br/>';
echo 'LINE : '.__LINE__ .'<br/>';
echo '<pre>';
var_dump( empty( $this->model_account_region ) );
echo '</pre>';
exit;

答案 1 :(得分:1)

model_account_region模型类搞砸了。因为你的时间不多了。我建议你复制功能

    public function getRegion($region_id) {
         $query = $this->db->query("SELECT DISTINCT *, (SELECT GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR '&nbsp;&nbsp;&gt;&nbsp;&nbsp;') FROM " . DB_PREFIX . "region_path cp LEFT JOIN " . DB_PREFIX . "region_description cd1 ON (cp.path_id = cd1.region_id AND cp.region_id != cp.path_id) WHERE cp.region_id = c.region_id AND cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' GROUP BY cp.region_id) AS path, (SELECT DISTINCT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'region_id=" . (int)$region_id . "') AS keyword FROM " . DB_PREFIX . "region c LEFT JOIN " . DB_PREFIX . "region_description cd2 ON (c.region_id = cd2.region_id) WHERE c.region_id = '" . (int)$region_id . "' AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'");

     return $query->row;
    }

进入model_profile_profile然后从那里调用它,即

    $region_info = $this->model_profile_profile->getRegion($region_id); // LINE 81

如果失败,则db类无法正确加载,因此在第81行调用函数之前添加此行

    $this->load->database();

如果您面临更多挑战,请告诉我。我在这里有点无聊。