Yii2 - Doesn't matter to execute a query? if yes, than

I am going to execute the below query in yii2.

$result = MyModel::find()->where(['res_model'=>$res_model])->all();

What is the difference between the above request and one request:

$result = MyModel::find()->where( 'res_model = :res_model',[
                    ':res_model' => $res_model] )->all();

both work fine, but a bit confusing both performance and usability. can someone please clarify on these please ??

+4
source share
2 answers

The same requests will be executed.
The first version is useful if you have several key values. The second version can be used if you need to complete a more complex query. Something like that

$result = MyModel::find()->where('some_field = (select max(date) from another_table where another_field=:param)',['param' => $param])->all();

Interoperability and security are the same.

+2
source

sql/mysql ( shoul )

, .

  $result = MyModel::find()->where(['res_model'=>$res_model])->all();

tecnique,

  $result = MyModel::find()->where( 'res_model = :res_model',[
                ':res_model' => $res_model] )->all();
0

Source: https://habr.com/ru/post/1662527/


All Articles