Aleš
Aleš

Reputation: 791

Correct use of Where in Query in YII2

I try to limit search result in Yii2 using Query class and where condition

$command=(new Query())          
         ->select('*')
         ->from('ct_ostatniNaklady')
         ->where('pid=:pid',[':pid' => 1])
         ->createCommand()->sql;

        var_dump($command);die;

However where command is not working properly. I get this result when I dump $command:

 string(48) "SELECT * FROM `ct_ostatniNaklady` WHERE pid=:pid" 

I don`t know why is :pid string not replacing by 1.

Using diffent syntax has no effect

 ->where(['pid'=>'1'])

result is quite common:

  string(50) "SELECT * FROM `ct_ostatniNaklady` WHERE `pid`=:qp0" 

What is correct way how to use where condition?

Upvotes: 2

Views: 841

Answers (1)

Bizley
Bizley

Reputation: 18021

This is absolutely normal, that is how PDO works. And you use where properly.

If you want to generate SQL query with every parameter bound to a variable value use:

...->createCommand()->rawSql;

PS. I hope you use it only for debugging and some special cases because usually there is no need to generate raw query like that.

Upvotes: 2

Related Questions