October 17, 2015, 4:26am
Hello in Guide 2.0 examples of like operator shows only how to compare the column value to a value type by a user or DBexpress.
For instance if I check that name column contains the word ‘tester’ I’ll write : [’like’, ’name’, ’tester’] will
generate name LIKE ’%tester%’.
But what if I wanna check that the string ‘my name is tester’ contains the value of the column? What I did is [’like’, ‘my name is tester’, ’name’] but I wonder if will not
generate ‘my name is tester’ LIKE ’%name%’.
So how can I tell the generator that the first is a value and the last is the column so check if the string has the value of the column?
October 17, 2015, 4:45am
I would like to use like operator in a raw sql instruction but I don’t know how to escape special characters(%…). Ex:
custormer::findBySql('Select customers.id where customers.name like %variableContainingValueWithSpecialCharacters%')->all();
Manually I can do it by regex replacing the special characters with escape + special characters before using the variable but is there any simple way(for example by calling a method or function passing the variable)
October 17, 2015, 6:25am
Please do not start another topic with the same subject.
October 17, 2015, 7:23am
I thought mixing them will make the question ambigous,ok.
October 17, 2015, 7:35am
Ah, sorry. I didn’t read them carefully.
As to the 2nd question, doesn’t it satisfy you?
like: operand 1 should be a column or DB expression, and operand 2 be a string or an array representing the values that the column or DB expression should be like. For example, [‘like’, ‘name’, ‘tester’] will generate name LIKE ‘%tester%’. When the value range is given as an array, multiple LIKE predicates will be generated and concatenated using AND. For example, [‘like’, ‘name’, [‘test’, ‘sample’]] will generate name LIKE ‘%test%’ AND name LIKE ‘%sample%’. The method will properly quote the column name and escape special characters in the values. Sometimes, you may want to add the percentage characters to the matching value by yourself, you may supply a third operand false to do so. For example, [‘like’, ‘name’, ‘%tester’, false] will generate name LIKE ‘%tester’.
So I think you could just write
->where('like', 'name', $variableContainingValueWithSpecialCharacters)
October 19, 2015, 8:18am
The problem is I wanna use it for inner join condition not in where.