开发者

How to apply a function in a where clause with doctrine 1.2

开发者 https://www.devze.com 2023-04-10 17:02 出处:网络
Imagine i want to search a phone number in my DB but they are saved with different formats Exemple : 12345678

Imagine i want to search a phone number in my DB but they are saved with different formats

Exemple : 12345678 13.36.15.36 13.654.365

What is the best way with doctrine to find a given phone.

I wish i could do something like this

Doctrine_Core::getTable('user')->create('u')->where(str_replace('.开发者_StackOverflow', '', u.phone) = ?, $phone)

Is there some kind of way to achieve this ?


I have tried using sql functions in the select method like concat and date_format. I don't see why replace wouldn't work.

Try this:

$q = Doctrine_Query::create()
     ->select("REPLACE(telnumber,'.','') as tel")
     ->from("user")
     ->where('tel = ?',123456);

To get your results in array format:

$q->execute(array(),Doctrine_Core::HYDRATE_ARRAY);


You can use mySQL Replace:

SELECT replace(`telnumber`, '.', '') as 'telnumbers' FROM `yourtable` where `telnumbers`=123456

Where telnumber is the field holding the telephone numbers, yourtable is the name of your table, and 123456 is the number you're looking for..

You can nest REPLACE statements if you wish to remove other characters from the telephone number field before searching, and/or also use telnumbers LIKE "%123456%" or similar...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号