Placeholders in ActiveQuery

How can I pass binding parameters to AQ?

I tried




$result = Concert::find()->select(['id', 'title'])->where(['like', 'title', ':term'])

    ->andFilterWhere(['!=', 'id', ':current'])->params([':term' => $term, ':current' => $current])

    ->asArray()->all();



Error:

I was noticed that, andFilterWhere don’t support passing parameters. Ok, next try:




$result = Concert::find()->select(['id', 'title'])->where(['like', 'title', ':term'])

    ->andWhere(['!=', 'id', ':current'])->params([':term' => $term, ':current' => $current])->asArray()->all();

    

$result = Concert::find()->select(['id', 'title'])->where(['like', 'title', ':term'])

    ->params([':term' => $term])->asArray()->all();


$result = Concert::find()->select(['id', 'title'])->where(['like', 'title', ':term'], [':term' => $term])

    ->andWhere(['!=', 'id', ':current'], [':current' => $current])->asArray()->all();



Same error…




$result = Concert::find()->select(['id', 'title'])->where(['title like :term', 'id <> :current'])

    ->params([':term' => $term, ':current' => $current])

    ->asArray()->all();



Exception ‘yii\base\InvalidParamException’ with message ‘Operator ‘TITLE LIKE :TERM’ requires two operands.’

You are right.




$result = Concert::find()->select(['id', 'title'])->where('title like :term AND id <> :current')

    ->params([':term' => $term, ':current' => $current])

    ->asArray()->all();



Fabrizio Caldarelli, Thank you!

Working example:




$result = Concert::find()->select(['id', 'title as text'])->where('title like :term AND id <> :current')

   ->params([':term' => '%'.$term.'%', ':current' => $current])->asArray()->all();



But, can I use the methods like andWhere and operand formats from docs?

PS: ‘:term’ => ‘%’.$term.’%’ seems horrible.

Try with:




$result = Concert::find()->select(['id', 'title'])->andWhere(['like', 'title', ':term'])

    ->andFilterWhere(['!=', 'id', ':current'])->params([':term' => $term, ':current' => $current])

    ->asArray()->all();