How do I view the parameters in a query?

后端 未结 5 1486
遇见更好的自我
遇见更好的自我 2021-02-08 04:09

In order to debug my code I would like to see the explicit sql query that is executed.

I create the query with createQueryBuilder, and the most explicit thi

5条回答
  •  广开言路
    2021-02-08 04:15

    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!

提交回复
热议问题