简体   繁体   中英

How to fix the following error in php?

I have a two piece of code for Regions and Categories. They are exactly the same. The code for Category works, but "Region" returns the following error:

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

And here is the code which probably causes the error:

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

I'd appreciate if you help me by giving a piece of code, because I'm dummy in PHP.

$this->model_account_region  //This property is null

Check

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

model_account_region model class is messed up. Since you are running out of time. I suggest you copy the function

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

into model_profile_profile then call it from there ie

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

if that fails its the db class failing to load properly so you add this line before calling your function on line 81

    $this->load->database();

let me know if you are facing more challenges. I am a bit bored over here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM