我根据教程使用 Zf2 组装了一个小型应用程序,我想从数据库中获取数据,我不得不面对一个奇怪的问题。
我的 TableGateway 类中有此方法:
public function selectWith($select = null) {
$filter = new Predicate();
$filter->like('category_name',"%dess%")
->OR
->like('category_desc', "%ks%");
var_dump($this->tableGateway->select(function(Select $select) use ($filter) {
$select->where($filter);
}));
return $this->tableGateway->select(function(Select $select) use ($filter) {
$select->where($filter);
});
}
根据转储,执行了以下查询:
SELECT `categories`.* FROM `categories` WHERE (`category_name` LIKE :where1 OR `category_desc` LIKE :where2)
而不是这个应该由源代码形式化:
SELECT `categories`.* FROM `categories` WHERE (`category_name` LIKE '%dess%' OR `category_desc` LIKE '%ks%')
这是一个错误,还是我错了什么?
根据 API 的 Like 只接受两个参数,都是字符串。
我使用 ZF 2.2.2
提前感谢您的任何帮助!
No, this is not a bug. Please check below how it works:<br />
在查看选择语句的变量转储之前
var_dump($this->tableGateway->select(function(Select $select) use ($filter) {
$select->where($filter);
}));
Please perform the var_dump of $filter. Where you will find the array of like statements.
example:
["predicates":protected]=>
array(2) {
[0]=>
array(2) {
[0]=>
string(3) "AND"
[1]=>
object(Zend'Db'Sql'Predicate'Like)#377 (3) {
["specification":protected]=>
string(14) "%1$s LIKE %2$s"
["identifier":protected]=>
string(12) "contact_name"
["like":protected]=>
string(4) "%dess%"
}
}
[1]=>
array(2) {
[0]=>
string(2) "OR"
[1]=>
object(Zend'Db'Sql'Predicate'Like)#378 (3) {
["specification":protected]=>
string(14) "%1$s LIKE %2$s"
["identifier":protected]=>
string(13) "business_name"
["like":protected]=>
string(4) "%ks%"
}
}
}
}
These two array's ["like":protected] portion are replaced with "where1" and "where2" respectively when the query is being executed. <br />
Thanks :)