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

后端 未结 19 2188
自闭症患者
自闭症患者 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:50

    getSqlQuery() does technically show the whole SQL command, but it's a lot more useful when you can see the parameters as well.

    echo $q->getSqlQuery();
    foreach ($q->getFlattenedParams() as $index => $param)
      echo "$index => $param";
    

    To make this pattern more reusable, there's a nice approach described in the comments at Raw SQL from Doctrine Query Object.

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

    You can easily access the SQL parameters using the following approach.

       $result = $qb->getQuery()->getSQL();
    
       $param_values = '';  
       $col_names = '';   
    
       foreach ($result->getParameters() as $index => $param){              
                $param_values .= $param->getValue().',';
                $col_names .= $param->getName().',';
       } 
    
       //echo rtrim($param_values,',');
       //echo rtrim($col_names,',');    
    

    So if you printed out the $param_values and $col_names , you can get the parameter values passing through the sql and respective column names.

    Note : If $param returns an array, you need to re iterate, as parameters inside IN (:?) usually comes is as a nested array.

    Meantime if you found another approach, please be kind enough to share with us :)

    Thank you!

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

    You can use :

    $query->getSQL();
    

    If you are using MySQL you can use Workbench to view running SQL statements. You can also use view the running query from mysql by using the following :

     SHOW FULL PROCESSLIST \G
    
    0 讨论(0)
  • 2020-11-30 19:57
    $sql = $query->getSQL();
    $obj->mapDQLParametersNamesToSQL($query->getDQL(), $sql);
    echo $sql;//to see parameters names in sql
    $obj->mapDQLParametersValuesToSQL($query->getParameters(), $sql);
    echo $sql;//to see parameters values in sql
    
    public function mapDQLParametersNamesToSQL($dql, &$sql)
    {
        $matches = [];
        $parameterNamePattern = '/:\w+/';
        /** Found parameter names in DQL */
        preg_match_all($parameterNamePattern, $dql, $matches);
        if (empty($matches[0])) {
            return;
        }
        $needle = '?';
        foreach ($matches[0] as $match) {
            $strPos = strpos($sql, $needle);
            if ($strPos !== false) {
                /** Paste parameter names in SQL */
                $sql = substr_replace($sql, $match, $strPos, strlen($needle));
            }
        }
    }
    
    public function mapDQLParametersValuesToSQL($parameters, &$sql)
    {
        $matches = [];
        $parameterNamePattern = '/:\w+/';
        /** Found parameter names in SQL */
        preg_match_all($parameterNamePattern, $sql, $matches);
        if (empty($matches[0])) {
            return;
        }
        foreach ($matches[0] as $parameterName) {
            $strPos = strpos($sql, $parameterName);
            if ($strPos !== false) {
                foreach ($parameters as $parameter) {
                    /** @var \Doctrine\ORM\Query\Parameter $parameter */
                    if ($parameterName !== ':' . $parameter->getName()) {
                        continue;
                    }
                    $parameterValue = $parameter->getValue();
                    if (is_string($parameterValue)) {
                        $parameterValue = "'$parameterValue'";
                    }
                    if (is_array($parameterValue)) {
                        foreach ($parameterValue as $key => $value) {
                            if (is_string($value)) {
                                $parameterValue[$key] = "'$value'";
                            }
                        }
                        $parameterValue = implode(', ', $parameterValue);
                    }
                    /** Paste parameter values in SQL */
                    $sql = substr_replace($sql, $parameterValue, $strPos, strlen($parameterName));
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 20:00

    To print out an SQL query in Doctrine, use:

    $query->getResult()->getSql();
    
    0 讨论(0)
  • 2020-11-30 20:03

    A working example:

    $qb = $this->createQueryBuilder('a');
    $query=$qb->getQuery();
    // SHOW SQL: 
    echo $query->getSQL(); 
    // Show Parameters: 
    echo $query->getParameters();
    
    0 讨论(0)
提交回复
热议问题