Kohana 2.3.4 ORM and Regex database method
I'm attempting to use Kohana's regex() database method with ORM to search for a word. I was using the like() method originally, but the results weren't what I wanted. Basically what I need to do is search a phrase for a certain word. I've used basic regular express开发者_如何学编程ions like this before, but I must be doing something wrong. I've tried the following without any success:
$prod_name = ORM::factory("product")->regex("prod_name", "^" . $searchArray[$i] . "$")->find_all();
$prod_name = ORM::factory("product")->regex("prod_name", "/b" . $searchArray[$i] . "/b")->find_all();
$prod_name = ORM::factory("product")->regex("prod_name", "/\b" . $searchArray[$i] . "\b/")->find_all();
Kohana's documentation states that regex() works the same as like(), but it's not. This works fine:
$prod_name = ORM::factory("product")->like("prod_name",$searchArray[$i])->find_all();
As does this:
$prod_name = ORM::factory("product")->like("prod_name",$searchArray[$i] . "%", FALSE)->find_all();
Through some further research, trial & error, I found the answer. Kohana's database method isn't using normal regular expressions, but MySql's version. In my situation, I'm searching for words in a phrase and would use the following (I'm using "shoe" as my example)
[[:<:]]shoe
Would find words that begin with shoe
shoe[[:>:]]
Would find words that end with shoe
[[:<:]]shoe[[:>:]]
Would only find words that are shoe
The code that ended up working for me is:
$prod_name = ORM::factory("product")->regex("prod_name", "[[:<:]]" . $searchArray[$i])->find_all();
Hope this helps someone else.
精彩评论