We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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().



5.9k

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



79.0k
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