Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

PDO Connection id set to 0

I have this class :

<?php

class Testmysql extends \Phalcon\Db\Adapter\Pdo\Mysql {

        static $id = null;
/**
 * Creates the Adapter
 */
public function __construct(array $descriptor = NULL)
{
    self::$id = uniqid(true);

    error_log(self::$id."\tMySQL::__construct(coucou)\n", 3, '/usr/share/nginx/html/test.log'); 

    return parent::__construct($descriptor);
}

/**
 * Connect
 */
public function connect(array $descriptor = NULL)
{
    error_log("MySQL::connect()\n", 3, '/usr/share/nginx/html/test.log');
    return parent::connect($descriptor);
}

/**
 * Close
 */
public function close()
{
    error_log(self::$id."\tMySQL::close(".$this->getConnectionId().")\n", 3, '/usr/share/nginx/html/test.log');
    return parent::close();
}

}

In log, the close function, i have 0 for each connection id, how is it possible ? thank you

Phalcon 3.0

edited Sep '16
public string getConnectionId () inherited from Phalcon\Db\Adapter

Gets the active connection unique identifier

In parent adapter class this property is defined as:

protected static _connectionConsecutive = 0;

So, on first run, it will be 0. It should increment later if you call connect().



1.7k

I called « connect() » before « close() » so why i have 0 ?



49.6k
Accepted
answer
edited Sep '16

You will always have connection ID = 0 on first instance.

The thing is, you need to instantiate an object again and again. So this is just a simple internal counter, w/o much practical use.

Simple example:

 $class = '\Phalcon\Db\Adapter\Pdo\\'.$adapter;  // adapter = MySQL, dbConfig = config opts

    $con = new $class($dbConfig);
    echo $con->getConnectionId(); //0

    $con = new $class($dbConfig);
    echo $con->getConnectionId(); //1

    $con = new $class($dbConfig);
    echo $con->getConnectionId(); //2

    $con = new $class($dbConfig);
    echo $con->getConnectionId(); //3

    $con = new $class($dbConfig);
    echo $con->getConnectionId(); //4

    $con = new $class($dbConfig);
    echo $con->getConnectionId(); //5

    $con = new $class($dbConfig);
    echo $con->getConnectionId(); //6

    $con = new $class($dbConfig);
    echo $con->getConnectionId(); //7

    $con = new $class($dbConfig);
    echo $con->getConnectionId(); //8