mruz / base-app

The base application in PhalconPHP
63 stars 19 forks source link

NFR - Allow SQL Logging & Debugging options #42

Open amsharma9 opened 8 years ago

amsharma9 commented 8 years ago

Hello,

I wanted to request you to add the facility to log SQL statements in your framework. See this: https://docs.phalconphp.com/en/latest/reference/models.html#logging-low-level-sql-statements

I have this in my Base App based config.ini

[app]
name = "base-app"
...
enableDebugging = 1
enableDblogging = 0

Or we can use just Debug & DBLog.

Kindly also provide Debugging boolean for code. We can place our echos, var_dump, \Phalcon\Debug\Dump calls in if (enableDebugging) { ...

Thanks Amal

mruz commented 8 years ago

Hi Amal, There is a Listener.php in the app/common/extension directory, so you can attach it to Events Manager:

$eventsManager = new \Phalcon\Events\Manager();
$eventsManager->attach('db', new \Baseapp\Extension\Listener());
$this->db->setEventsManager($eventsManager);

and run some query to debug:

$query = $this->db->convertBoundParams('SELECT * FROM `users` WHERE `user_id` = :user_id:', array(':user_id' => 1));
$user = $this->db->fetchAll($query['sql'], \Phalcon\Db::FETCH_ASSOC, $query['params']);

or you can add it in the Bootstrap if the env is development:

protected function db()
{
    $config = $this->_config;

    $db = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => $config->database->host,
        "username" => $config->database->username,
        "password" => $config->database->password,
        "dbname" => $config->database->dbname,
        "options" => array(
            \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
        )
    ));

    if ($config->app->env == "development") {
        $eventsManager = new \Phalcon\Events\Manager();
        $eventsManager->attach('db', new \Baseapp\Extension\Listener());
        $db->setEventsManager($eventsManager);
    }

    $this->_di->set('db', $db);
}