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

Phalcon\Db\Profiler\Item->getSQLVariables appears to always be empty.

Expected and Actual Behavior

\Phalcon\Db\Profiler\Item->getSQLVariables() is expected to return a list of variables used in the profile's SQL statement bound parameters. This doesn't appear to be true.

Relevant di configuration:

$di->setShared('profiler', function () {
    return new \Phalcon\Db\Profiler();
});
$di->setShared('db', function() use ($di) {
    $connection = new Connection(array(
        "host" => $db_creds->host,
        "username" => $db_creds->username,
        "password" => $db_creds->password,
        "dbname" => $db_creds->dbname,
    ));
    $eventsManager = new Phalcon\Events\Manager();
    $profiler = $di->getProfiler();
    $eventsManager->attach('db', function ($evt, $conn) use ($profiler) {
        if ($evt->getType() === "beforeQuery") {
            $profiler->startProfile($conn->getSQLStatement());
        }
        if ($evt->getType() === "afterQuery") {
            $profiler->stopProfile();
        }
    });
    $connection->setEventsManager($eventsManager);
    return $connection;
});

This is in a controller afterExecuteRoute()

// in the example below, an insert was successful on the books table at this point.
$profiles = $this->profiler->getProfiles();
foreach ($profiles as $profile) {
    error_log(
        "SQL Statement: ". $profile->getSQLStatement() .
        " with variables " . $profile->getSQLVariables() .
        " Total Elapsed Time: ". $profile->getTotalElapsedSeconds()
    );
}

Output:

[Wed Oct 19 15:17:22.804249 2016] [:error] [pid 23792] [client 98.191.80.75:47708] SQL Statement: INSERT INTO `books` (`full_name`, `summary`, `vendor_id`, `category_tag_id`, `date_created`, `banner`, `thumbnail`, `activated`) VALUES (?, ?, ?, ?, ?, ?, ?, DEFAULT) with variables  Total Elapsed Time: 0.00037097930908203, referer: REDACTED

Details

Phalcon version: (php --ri phalcon)

~$ php --ri phalcon

phalcon

Web framework delivered as a C-extension for PHP
phalcon => enabled
Author => Phalcon Team and contributors
Version => 3.0.1
Build Date => Sep  1 2016 18:08:54
Powered by Zephir => Version 0.9.4a-dev-7e304ba18c

Directive => Local Value => Master Value
phalcon.db.escape_identifiers => On => On
phalcon.db.force_casting => Off => Off
phalcon.orm.events => On => On
phalcon.orm.virtual_foreign_keys => On => On
phalcon.orm.column_renaming => On => On
phalcon.orm.not_null_validations => On => On
phalcon.orm.exception_on_failed_save => Off => Off
phalcon.orm.enable_literals => On => On
phalcon.orm.late_state_binding => Off => Off
phalcon.orm.enable_implicit_joins => On => On
phalcon.orm.cast_on_hydrate => Off => Off
phalcon.orm.ignore_unknown_columns => Off => Off

PHP Version: (php -v)

~$ php -v
PHP 5.5.9-1ubuntu4.20 (cli) (built: Oct  3 2016 13:00:37) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with the ionCube PHP Loader v4.7.5, Copyright (c) 2002-2014, by ionCube Ltd., and
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

Operating System:

~$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"

Installation type:

~$ cat /etc/apt/sources.list.d/phalcon_stable.list 
deb https://packagecloud.io/phalcon/stable/ubuntu trusty main

Zephir version (if any): N/A

Server:

~$ apache2 -version
Server version: Apache/2.4.7 (Ubuntu)
Server built:   Jul 15 2016 15:34:04

Other related info (Database, table schema):

~$ mysql --version
mysql  Ver 14.14 Distrib 5.5.52, for debian-linux-gnu (x86_64) using readline 6.3


7.5k
Accepted
answer

Upon further research, to get the variables into the profile, apparently you need to pass said variables into the profiler when you start the profile:

    $eventsManager->attach('db', function ($evt, $conn) use ($profiler) {
        if ($evt->getType() === "beforeQuery") {
            $profiler->startProfile($conn->getSQLStatement(), $conn->getSQLVariables());
        }

        if ($evt->getType() === "afterQuery") {
            $profiler->stopProfile();
        }
    });