Query registration with PDO - autocomplete not working

I am trying to create an intermediate class that will register requests in an array along with their runtime. Everything is in order and everything works perfectly. But autocomplete doesn't work when I try to access an intermediate class. How to make autocomplete work. I am using Netbeans.

An intermediate class name is a model.

From my application, I have a class called Users that extends the 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 } } 

The My Model class is as follows.

 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 ); } } 

Do they have a better way to do this?

+4
source share
1 answer

I fixed this by accepting suggestions from @cillosis and @Touki

@Touki, I agree, I should not extend the PDO class. @cillosis, thanks for your comments.

So I wrote my class. I have not yet inserted the complete code until it is complete. But I checked his work. And I can also register my requests. However, I am not sure that I can register the runtime.

 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); } } 
+1
source

All Articles