How to use the text property of a CDbCommand object ??




$id = 1;   //just as example

$sql = "SELECT * FROM sometable WHERE id = :id";

$command = Yii::app()->db->createCommand($sql); 

$command = $command->bindValue(':id', $id);

CHtmlEx::out( $command->queryAll() );    //this CHtmlEx is just a helper utility class, and out is <pre> print_r() </pre>

print $command->text;



Although the query works fine the last line is not displaying the value 1 inside the WHERE

Shouldn’t I see this: SELECT * FROM sometable WHERE id = 1 ???

instead I simply get my original sql string SELECT * FROM sometable WHERE id = :id

I wanted to feed the final SQL stament using bindValue (or bindParam) into CSqlDataProvider which takes a string as a first parameter.

Is this possible in the first place?

and how? :)

Thank you!

Ok excuse me about that I neglected the ‘params’ property of the CSqlDataProvider

Although it is still a question how could you get the text of a final sql statement which was prepared (before performing the query of course)

I know it is not very crucial but If you know the answer please share :)

That is, by the nature of prepared statements, not possible. Prepared statements are executed in two steps: the driver first sends the statement (with placeholders) to the DBMS, then sends the values for placeholders. The latter can be repeated with different values as many times as needed. You can use str_replace() or similar if you really need it, but I don’t think so :)

EDIT: db component can be configured with:




'enableProfiling' => true,

'enableParamLogging' => true,



You can use CWebLogRoute then, to display all SQL queries with the list of bounded parameters.