MySQL Doctrine 的 findBy*() 中的 LIKE 和 % 通配符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2710181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LIKE and % Wildcard in Doctrine's findBy*()
提问by Anthony
How do I write the following MySQL query using Doctrine's findBy*() method?:
如何使用 Doctrine 的 findBy*() 方法编写以下 MySQL 查询?:
SELECT column_name1, column_name2 FROM table_name
WHERE column_name3 LIKE '%search_key%';
For Example, to fetch multiple rows from a column named "ColumnName" (below) using Doctrine:
例如,要使用 Doctrine 从名为“ColumnName”(如下)的列中获取多行:
$users = Doctrine::getTable('User')->findByColumnName('active');
echo $users[0]->username;
echo $users[1]->username;
I tried:
我试过:
$search_key = 'some value';
$users = Doctrine::getTable('User')->findByColumnName('%$search_key%');
echo $users[0]->username;
echo $users[1]->username;
and I got no errors, but nothing displayed.
我没有错误,但没有显示任何内容。
Any assistance will be really appreciated. Thanks in advance.
任何帮助将不胜感激。提前致谢。
回答by Amy B
$users = Doctrine::getTable('User')->createQuery('u')
->where('column_name3 LIKE ?', '%search_key%')
->execute();