Search code examples
opencart-3

When inserting data in OpenCart 3.x, Uncaught Error: Call to a member function coming


I have created a model and controller for dealer entries in cart using OpenCart 3.x. I'm getting the following error when submitting the form.

Fatal error: Uncaught Error: Call to a member function addDealer() on null in C:\xampp\htdocs\mywebsite\catalog\controller\checkout\quotation.php:27 Stack trace: #0 C:\xampp\storage\modification\system\engine\action.php(79): ControllerCheckoutQuotation->index() #1 C:\xampp\htdocs\mywebsite\catalog\controller\startup\router.php(25): Action->execute(Object(Registry)) #2 C:\xampp\storage\modification\system\engine\action.php(79): ControllerStartupRouter->index() #3 C:\xampp\htdocs\mywebsite\system\engine\router.php(67): Action->execute(Object(Registry)) #4 C:\xampp\htdocs\mywebsite\system\engine\router.php(56): Router->execute(Object(Action)) #5 C:\xampp\htdocs\mywebsite\system\framework.php(169): Router->dispatch(Object(Action), Object(Action)) #6 C:\xampp\htdocs\mywebsite\system\startup.php(104): require_once('C:\\xampp\\htdocs...') #7 C:\xampp\htdocs\mywebsite\index.php(19): start('catalog') #8 {main} thrown in C:\xampp\htdocs\mywebsite\catalog\controller\checkout\quotation.php on line 27

Here is the model code,

class ModelCheckoutQuotation extends Model {
    public function addDealer($data) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "dealer SET store_id = '" . (int)$this->config->get('config_store_id') . "', language_id = '" . (int)$this->config->get('config_language_id') . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', ip = '" . $this->db->escape($this->request->server['REMOTE_ADDR']) . "', status = 1, date_added = NOW()");

        $dealer_id = $this->db->getLastId(); 
        
        return $dealer_id;
    }
}

Here is the controller code

if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {

    $dealer_id = $this->model_checkout_quotation->addDealer($this->request->post);

    $this->response->redirect($this->url->link('checkout/quotesuccess'));
}

I placed the controller in catalog/controller/checkout/quotation.php and model in catalog/model/checkout/quotation.php

I don't know why the error is occurring. There is no database connectivity issues.

Thanks


Solution

  • You must load model in your controller file before:

    $this->load->model('checkout/quotation');
    
    if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            $dealer_id = $this->model_checkout_quotation->addDealer($this->request->post);
      $this->response->redirect($this->url->link('checkout/quotesuccess'));
    }