j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

Issue with select_expr() and logging? #23

Closed philoye closed 11 years ago

philoye commented 13 years ago

I'm running in an issue that only manifests when logging is on:

     select_expr("FROM_UNIXTIME(`time`, '%Y-%c')", 'month')

I get a php warning:

    vsprintf(): Too few arguments in <strong><code>/path/to/idiorm/idiorm.php</code></strong> line <strong><code>267</code></strong>

A little examination reveals that the $parameters array in _log_query() function gets an extra item that is undefined. Not sure what is happening though. It doesn't happen with a simpler expression, like COUNT().

j4mie commented 13 years ago

Hi Phil,

Sorry I've been a bit slow responding to these issues - hoping to catch up in the next couple of weeks.

Pretty sure I know what's causing that problem, but will need a bit of thought to fix.

Cheers

Jamie

majman commented 12 years ago

any update on this issue? running into a similar problem.

andyhofman commented 12 years ago

Ok, I came up with a solution. Before replacing %s, I replace all % with &#37. Works like a charm.

Replace function _log_query with the function below.

    protected static function _log_query($query, $parameters) {
        // If logging is not enabled, do nothing
        if (!self::$_config['logging']) {
            return false;
        }

        if (count($parameters) > 0) {
            // Escape the parameters
            $parameters = array_map(array(self::$_db, 'quote'), $parameters);

            // Replace placeholders in the query for vsprintf
            $query = str_replace("%", "&#37", $query);
            $query = str_replace("?", "%s", $query);

            // Replace the question marks in the query with the parameters
            $bound_query = vsprintf($query, $parameters);

        } else {
            $bound_query = $query;
        }

        self::$_last_query = $bound_query;
        self::$_query_log[] = $bound_query;
        return true;
    }
treffynnon commented 11 years ago

This is fixed in commit 84bd58f902d93970a6626c2e3d360eed09a4b174