YII CDBCommand getText to show all variables in SQL

I am using Yii Yii :: app () -> db-> createCommand () to create an SQL query. To view the SQL code that Yii generates, I use the getText () method for CDBCommand. The problem is that I am using the getText () method for an SQL code that contains parameters, for example:

Yii::app()->db->createCommand() ->select("name") ->from('package') ->where('id=:id', array(':id'=>5)) ->queryRow(); 

The getText () method returns the following SQL:

 select name from package where id=:id 

instead:

 select name from package where id=5 

This is good for simple queries, but for more complex queries with a large number of parameters, it is rather painful to copy / paste each parameter into the SQL code to check it.

Is there a way to show parameters directly inside SQL using getText () or some other method in Yii?

Hooray!

+7
source share
4 answers

It looks like you're after the PDOStatement that Yii is preparing:

 $cmd = Yii::app()->createCommand(); $cmd->select("name") ->from('package') ->where('id=:id', array(':id'=>5)) ->queryRow(); // returns a PDO Statement - http://php.net/manual/en/class.pdostatement.php Yii::log($cmd->getPdoStatement()->queryString); 

Does this work for you? It seems like it should (untested code).

-3
source
 $sql = Yii::app()->db->createCommand() ->select("name") ->from('package') ->where('id=:id', array(':id'=>5)) ->queryRow(); $query=str_replace( array_keys($sql->params), array_values($sql->params), $sql->getText() ); 
+4
source

You can use the CDbConnetion :: enableParamLogging property. For example, in config / main.php:

 'db' => array ( 'enableParamLogging' => true, 

and the displayed and logged error will contain related values.

+2
source

why are you trying as below. I'm not an expert, just send a message, as far as I know, if it is not suitable for your problem, forgive me ...

  $connection=Yii::app()->db; $id=5; // you can able to change by "GET" or "POST" methods $sql="SELECT name FROM package WHERE id = :id "; $command = $connection->createCommand($sql); $command->bindParam(":id",$id,PDO::PARAM_STR); $dataReader=$command->query(); $rows=$dataReader->readAll(); $namevalue=array(); foreach($rows as $max) { $namevalue = $max['name']; } echo $namevalue; // which is the value u need 

thanks...

0
source

All Articles