Search code examples
phpmysqlopencartopencart-3

How to check last executed query in Opencart3


$data['module_status'] = $this->config->get('visit_module_enabled');

I need to check what query is executed in $this->config->get. And anyhow, just like code Igniter has an option $this->db->last_query() , don't we have something similar in Opencart3 ?

If this is the original Db object, are those really all the functions we have for $this->db object? https://github.com/opencart/opencart/blob/master/upload/system/library/db.php

Or perhaps some implementation in core PHP or mysql to achieve the same?


Solution

  • ... just like code Igniter has an option $this->db->last_query() , don't we have something similar in Opencart3 ?

    No. Opencart is not a framework (like CodeIgniter) and doesn't offer as robust a library of functions.

    If this is the original Db object, are those really all the functions we have for $this->db object?

    Yes.

    I need to check what query is executed in $this->config->get

    That doesn't make any sense. $this->config->get() simply gets the value of an array key of the $data property of the config class. All of those properties are loaded on site initialization. Most of them (not all) come from this query in controller/startup/startup.php:

    SELECT * FROM `" . DB_PREFIX . "setting` WHERE store_id = '0' OR store_id = '" . (int)$this->config->get('config_store_id') . "' ORDER BY store_id ASC
    

    If you want to know where that specific value came from it's most likely going to be in the setting table where key = 'visit_module_enabled'.

    If you still want to create a last_query() method, it would be very easy, but based on your question I don't think it's going to get you what you want.

    To create a last_query() method, you can modify the db class like this:

    private $last_query;
    
    public function query($sql) {
        $this->last_query = $sql;
        return $this->adaptor->query($sql);
    }
    
    public function last_query() {
        return $this->last_query;
    }