Doctrine - How to print out the real sql, not just the prepared statement?

后端 未结 19 2189
自闭症患者
自闭症患者 2020-11-30 18:59

We\'re using Doctrine, a PHP ORM. I am creating a query like this:

$q = Doctrine_Query::create()->select(\'id\')->from(\'MyTable\');

相关标签:
19条回答
  • 2020-11-30 19:44

    Modified @dsamblas function to work when parameters are date strings like this '2019-01-01' and when there is array passed using IN like

    $qb->expr()->in('ps.code', ':activeCodes'),
    

    . So do everything what dsamblas wrote, but replace startQuery with this one or see the differences and add my code. (in case he modified something in his function and my version does not have modifications).

    public function startQuery($sql, array $params = null, array $types = null)
    
    {
        if($this->isLoggable($sql)){
            if(!empty($params)){
                foreach ($params as $key=>$param) {
    
                    try {
                        $type=Type::getType($types[$key]);
                        $value=$type->convertToDatabaseValue($param,$this->dbPlatform);
                    } catch (Exception $e) {
                        if (is_array($param)) {
                            // connect arrays like ("A", "R", "C") for SQL IN
                            $value = '"' . implode('","', $param) . '"';
                        } else {
                            $value = $param; // case when there are date strings
                        }
                    }
    
                    $sql = join(var_export($value, true), explode('?', $sql, 2));
                }
    
            }
            echo $sql . " ;".PHP_EOL;
        }
    }
    

    Did not test much.

    0 讨论(0)
  • 2020-11-30 19:45

    There is no other real query, this is how prepared statements work. The values are bound in the database server, not in the application layer.

    See my answer to this question: In PHP with PDO, how to check the final SQL parametrized query?

    (Repeated here for convenience:)

    Using prepared statements with parametrised values is not simply another way to dynamically create a string of SQL. You create a prepared statement at the database, and then send the parameter values alone.

    So what is probably sent to the database will be a PREPARE ..., then SET ... and finally EXECUTE ....

    You won't be able to get some SQL string like SELECT * FROM ..., even if it would produce equivalent results, because no such query was ever actually sent to the database.

    0 讨论(0)
  • 2020-11-30 19:46

    Doctrine is not sending a "real SQL query" to the database server : it is actually using prepared statements, which means :

    • Sending the statement, for it to be prepared (this is what is returned by $query->getSql())
    • And, then, sending the parameters (returned by $query->getParameters())
    • and executing the prepared statements

    This means there is never a "real" SQL query on the PHP side — so, Doctrine cannot display it.

    0 讨论(0)
  • 2020-11-30 19:46

    Maybe it can be useful for someone:

    // Printing the SQL with real values
    $vals = $query->getFlattenedParams();
    foreach(explode('?', $query->getSqlQuery()) as $i => $part) {
        $sql = (isset($sql) ? $sql : null) . $part;
        if (isset($vals[$i])) $sql .= $vals[$i];
    }
    
    echo $sql;
    
    0 讨论(0)
  • 2020-11-30 19:47

    You can check the query executed by your app if you log all the queries in mysql:

    http://dev.mysql.com/doc/refman/5.1/en/query-log.html

    there will be more queries not only the one that you are looking for but you can grep for it.

    but usually ->getSql(); works

    Edit:

    to view all the mysql queries I use

    sudo vim /etc/mysql/my.cnf 
    

    and add those 2 lines:

    general_log = on
    general_log_file = /tmp/mysql.log
    

    and restart mysql

    0 讨论(0)
  • 2020-11-30 19:47
    Solution:1
    ====================================================================================
    
    function showQuery($query)
    {
        return sprintf(str_replace('?', '%s', $query->getSql()), $query->getParams());
    }
    
    // call function  
    echo showQuery($doctrineQuery);
    
    Solution:2
    ====================================================================================
    
    function showQuery($query)
    {
        // define vars              
        $output    = NULL;
        $out_query = $query->getSql();
        $out_param = $query->getParams();
    
        // replace params
       for($i=0; $i<strlen($out_query); $i++) {
           $output .= ( strpos($out_query[$i], '?') !== FALSE ) ? "'" .str_replace('?', array_shift($out_param), $out_query[$i]). "'" : $out_query[$i];
       }
    
       // output
       return sprintf("%s", $output);
    }
    
    // call function  
    echo showQuery($doctrineQueryObject);
    
    0 讨论(0)
提交回复
热议问题