就像在 zend 框架 2 中的 where 查询中一样 [英] like in where query in zend framework 2
本文介绍了就像在 zend 框架 2 中的 where 查询中一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Zend 框架 2.x 并面临这个问题,因为我进行了大量搜索.我想在查询中使用 like 子句,但每次都会给出错误:
I am using the Zend framework 2.x and facing the problem as I have search a lot. I want to use the like clause in query but each time gives the errors:
这是我的努力:
$sql = new Sql($this->adapter);
$select = $sql->select()->columns(
array('user_profile_id', 'profile_login_name'))->from($this->table)->where->like(
'profile_login_name', '%'.$strSearch.'%');
echo $select->getSqlString(); die;
但这给出了错误:
致命错误:调用未定义的方法Zend\Db\Sql\Where::getSqlString() 在/var/www/YAAB/branches/admin/models/Model/UserTable.php 上线131
Fatal error: Call to undefined method Zend\Db\Sql\Where::getSqlString() in /var/www/YAAB/branches/admin/models/Model/UserTable.php on line 131
我也使用过 Zend\Db\Sql\Predicate 但这也给出了错误.
I have also used the Zend\Db\Sql\Predicate but this also gives the error.
所以我的问题是
- 如何在 Zend 框架 2 中的查询中使用 like 子句?
- 我的代码有什么问题?
有急事请尽快回复.
推荐答案
试试这个
$select = $sql->select(); // or new Select('table');
$where = new \Zend\Db\Sql\Where();
// Using predicates
$where->addPredicate(
new \Zend\Db\Sql\Predicate\Like('my_field', '%test%')
);
// Alternatively, a shortcut
$where->like('my_field', '%test%'); // Alternatively, a shortcut.
$select->where($where);
// this part will depend on if you're using TableGateway or what ever
$stmt = $sql->prepareStatementForSqlObject($select);
$resultSet = new ResultSet();
$resultSet->initialize($stmt->execute());
这篇关于就像在 zend 框架 2 中的 where 查询中一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文