[英]How to apply a function in a where clause with doctrine 1.2
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 范例: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('.', '', 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. 我已经尝试在concat和date_format之类的select方法中使用sql函数。 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 : 您可以使用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.. 其中telnumber
是保存电话号码的字段, yourtable
是表的名称, 123456
是您要查找的号码。
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... 如果希望在搜索之前从电话号码字段中删除其他字符,则可以嵌套REPLACE语句,并且/或者也可以使用电话号码, telnumbers LIKE "%123456%"
或类似的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.