Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:24:26+00:00 2026-06-16T04:24:26+00:00

I am trying to make an intermediate class which will log the queries in

  • 0

I am trying to make an intermediate class which will log the queries in an array along with their execution time. Everything is fine and it works perfectly. But autocomplete doesnt work when i try to access the intermediate class. How can get the autocomplete to work. I am using Netbeans.

Intermediate classname is Model.

From my application, i have a class by the name Users which extends Model.

class Users extends Model
{
    function __construct() {
        parent::__construct();
        $stmt = $this->prepare('SELECT * FROM users WHERE id=? ');

        $stmt->bindValue(1, 1); //$stmt-> auto-complete is unavailable
        $stmt->execute();

        $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

        print_r($rows); //i get results
    }
}

My Model class looks like this.

class Model extends PDO
{
    public static $log = array();
    private $query_cache = array();

    public function __construct() {
        parent::__construct(
            "mysql:dbname=".MYSQL_DB.";host=".MYSQL_HOST,
            MYSQL_USER, MYSQL_PASSWORD
        );
    }

    public function query($query) {
        $time = "";
        $query = mysql_real_escape_string(preg_replace( '/\s+/', ' ', $query ));

        if (key_exists($query,$this->query_cache)
            && is_object($this->query_cache[$query]))
        {
            $result = $this->query_cache[$query];
        } else {
            $start = microtime(true);
            $result = parent::query($query);
            $time = microtime(true) - $start;
            $this->query_cache[$query] = $result;
            Logger::$logText['DATABASE'][] = array(
                'QUERY' => $query,
                'TIME'  => number_format($time,4)
            );
        }
        return $result;
    }

    /**
     * @return LoggedPDOStatement
     */
    public function prepare($query) {
        return new LoggedPDOStatement(parent::prepare($query));
    }
}

My LoggedPDOStatement looks like this.

class LoggedPDOStatement
{
    /**
     * The PDOStatement we decorate
     */
    private $statement;

    public function __construct(PDOStatement $statement) {
        $this->statement = $statement;
    }

    /**
     * When execute is called record the time it takes and
     * then log the query
     * @return PDO result set
     */
    public function execute() {
        $start = microtime(true);
        $result = $this->statement->execute();
        $time = microtime(true) - $start;
        Model::$log[] = array(
            'query' => '[PS] ' . $this->statement->queryString,
            'time'  => round($time * 1000, 3)
        );
        return $result;
    }
    /**
     * Other than execute pass all other calls to the PDOStatement object
     * @param string $function_name
     * @param array $parameters arguments
     */
    public function __call($function_name, $parameters) {
        return call_user_func_array(
            array($this->statement, $function_name), $parameters
        );
    }
}

Is their any better way of doing this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:24:27+00:00Added an answer on June 16, 2026 at 4:24 am

    I have fixed this taking suggestions from @cillosis and @Touki

    @Touki, i agree i shouldnt extend the PDO Class.
    @cillosis, thanks for your comments.

    This is the way i have written my class. I have not pasted the full code as its not complete yet. But i have checked it works. And i can log my queries as well. However i am not sure if i will be able to log the execution time.

    class Model
    {
        /**
         * The singleton instance
         *
         */
        static private $PDOInstance;
    
        public function __construct($dsn="", $username = false, $password = false, $driver_options = false) {
            if (!self::$PDOInstance) {
                try {
                    self::$PDOInstance = new PDO(
                        "mysql:dbname=".MYSQL_DB.";host=".MYSQL_HOST,
                        MYSQL_USER, MYSQL_PASSWORD
                    );
                } catch (PDOException $e) {
                    die("PDO CONNECTION ERROR: " . $e->getMessage() . "<br/>");
                }
            }
            return self::$PDOInstance;
        }
    
        /**
         * Initiates a transaction
         *
         * @return bool
         */
        public function beginTransaction() {
            return self::$PDOInstance->beginTransaction();
        }
    
        public function prepare($statement, $driver_options = false) {
            //log the $statement
            if (!$driver_options)
                $driver_options = array();
            return self::$PDOInstance->prepare($statement, $driver_options);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a asp.net application calling intermediate C# dll which will make a call
I'm trying make a login window where a user is prompted to enter their
I am fairly new to iOS development and trying make a simple app which
I have an abstract class in a library. I'm trying to make it as
I'm trying to make a simple intermediate compilation function: lst = [lambda x: calculate_urls(),
We have quite common code which worked fine: public class CompressionFilterAttribute : ActionFilterAttribute {
I'm trying make this code which is from a Dictionary of Arrays (CODE) from
I'm a beginner/intermediate at reverse engineering and I’m trying to make the leap to
I'm trying make an entity with doctrine that has three associations with other entities
I am trying make long screen to vertical direction. So, I need a screen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.