Install, uninstall and your own tables
Settings fit into the common setting table, but lists of records — callback requests, log entries — need a table of their own. OpenCart has no migrations. The table is created by the install() method of the admin controller and removed by uninstall().
When the methods are called
Button in Extensions → Extensions | Method |
|---|---|
| Install | install() |
| Uninstall | uninstall() |
Both methods live in the admin controller of the extension, next to index() and save().
A model for the SQL
The queries are put into an admin model of the extension. Create the file by hand:
// extension/acme_hello/admin/model/module/acme_hello.php
namespace Opencart\Admin\Model\Extension\AcmeHello\Module;
class AcmeHello extends \Opencart\System\Engine\Model
{
public function install(): void
{
$this->db->query(
"CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "acme_callback` (
`callback_id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(64) NOT NULL,
`phone` VARCHAR(32) NOT NULL,
`customer_id` INT NOT NULL DEFAULT 0,
`date_added` DATETIME NOT NULL,
PRIMARY KEY (`callback_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"
);
}
public function uninstall(): void
{
$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "acme_callback`");
}
}The table will hold callback requests that customers leave on the storefront — the form is written in a later lesson.
The controller methods
// extension/acme_hello/admin/controller/module/acme_hello.php
public function install(): void
{
$this->load->model('extension/acme_hello/module/acme_hello');
$this->model_extension_acme_hello_module_acme_hello->install();
}
public function uninstall(): void
{
$this->load->model('extension/acme_hello/module/acme_hello');
$this->model_extension_acme_hello_module_acme_hello->uninstall();
}The model of an extension is loaded by the route extension/acme_hello/module/acme_hello, and the property name is the same route with underscores.
The extension is already installed, so press Uninstall and then Install in Extensions → Extensions → Modules. Open phpMyAdmin — the table oc_acme_callback is in the list.
Reading the table
Add a method to the same model and call it from the settings page:
public function getCallbacks(): array
{
$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "acme_callback` ORDER BY `date_added` DESC LIMIT 20");
return $query->rows;
}// in index() of the admin controller
$this->load->model('extension/acme_hello/module/acme_hello');
$data['callbacks'] = $this->model_extension_acme_hello_module_acme_hello->getCallbacks();The template prints the array with a for loop under the settings form. The table is empty for now.
What the core removes itself
On uninstall the core deletes the settings saved with the code of the extension. Tables, events and cron tasks created by your install() are your responsibility: everything created there is removed in uninstall().
Common mistake
CREATE TABLE is written without IF NOT EXISTS. A repeated installation — after a failed first attempt or after copying the database — stops with the error «Table already exists». Write IF NOT EXISTS when creating and IF EXISTS when dropping.