JasonMortonNZ
JasonMortonNZ

Reputation: 3750

How to call a function from model in controller with an OpenCart module?

I hope this isn't to vague, I am still a beginner to the idea of the MVC-L framework. I'm in the process of creating an Open-cart 1.5.1.3 module, and from what I can gather OC 1.5+ will call the install() function automatically from within you controller when you first install the a module (if you provide install() function that is). Where I'm having trouble is with actually calling a function (which creates a new database table) which is located in the model from my controller function install().

Here is the code I have already:

Controller : TrademeXml

 public function install() {
    $this->load->model('model/TrademeXml');
// Create table to store TradeMe ID
$this->model_model_TradmeXml->createModuleTables();
}

Model : TradmeXml

 public function createModuleTables() {
    $query = $this->db->query("CREATE TABLE IF NOT EXISTS " . DB_PREFIX . "trademeID (tid INT(30), PRIMARY KEY(tid)");
}

The install function is called during the installation of the module, but I get the following error:

Fatal error: Call to a member function createModuleTables() on a non-object in D:\xampp\htdocs\store\admin\controller\module\TrademeXml.php

Upvotes: 1

Views: 6408

Answers (2)

hram908
hram908

Reputation: 394

I was missing this...

public function index() {
        $this->install();

Upvotes: 0

Jay Gilford
Jay Gilford

Reputation: 15151

$this->load->model('module/tradexml');
$this->model_module_tradexml->createModuleTables();

Upvotes: 4

Related Questions