Manohar singh
Manohar singh

Reputation: 509

How to write replace string function inside Yii2 query

I have a MySQL query which selects column based on the replace string values passed to a column

SELECT * FROM Table WHERE REPLACE(column_name, '.', '') = 'value';

In the above query i remove dot values from the column_name of the table and I pass values with out dots and it works fine.

Now i need to make this query to work in Yii2 query

I have a query in Yii2 like below,

$Table= Table::findOne([ column_name => \Yii::$app->request->post('value')]);

How can i make the first query condition work in Yii2 query?

Upvotes: 2

Views: 1977

Answers (2)

vishuB
vishuB

Reputation: 4261

Use Like

$Table= Table::find()
      ->where(["REPLACE(column_name, '.', '')" => \Yii::$app->request->post('value')])
      ->one();

Refer Yii2 DB Query Builder Guide

Upvotes: 2

ScaisEdge
ScaisEdge

Reputation: 133360

a simple way is the use of string format in where condition

  $Table= Table::find()->where("REPLACE(column_name, '.', '') = 'value'")->one();

you can find useful sample in this guide

https://www.yiiframework.com/doc/guide/2.0/en/db-query-builder

Upvotes: 1

Related Questions