問題描述
我想弄清楚如何在 where 子句中使用 OR 進(jìn)行 Mysql Select.默認(rèn)情況下,where 語句中的所有子句都是 AND 運(yùn)算的,經(jīng)過幾個(gè)小時(shí)的嘗試和失敗,查看網(wǎng)絡(luò)無法使其工作.在 ZF1 中它將是 orWhere() 但在 ZF2 中沒有.
I am trying to figure out how to make a Mysql Select with an OR in where Clause. By default all clauses in where statement are ANDed and after some hours of try and fail and looking the net can't make it work. In ZF1 it would be an orWhere() but there is not in ZF2.
這是我在 AbstracttableGateway
中的代碼:
This is the code i have inside a AbstracttableGateway
:
$resultSet = $this->select(function (Select $select) use ($searchstring) {
$select->join('users','blog_posts.id_user = users.id',array('name'))
->join('blog_categories','blog_posts.id_category = blog_categories.id', array(
'cat_name' => 'name',
'cat_alias' => 'alias',
'cat_image' => 'icon'))
->order('date DESC');
//->where("title LIKE '%" .$searchstring . "%' OR content LIKE '%". $searchstring ."%'")
$select->where->like('content','%'.$searchstring.'%');
$select->where->like('title','%'.$searchstring.'%');
}
);
return $resultSet;
帶有 $select->where->like
的行是 AND 運(yùn)算的,我希望它們帶有 OR.我應(yīng)該改變什么?
the lines with the $select->where->like
are the ones that are ANDed and I want them with OR. What should I change?
推薦答案
ZF2 中沒有 orWhere
,但你可以使用 ZendDbSqlPredicate
的任意組合代碼>對(duì)象.對(duì)于 OR,在 PredicateSet
和 PredicateSet::COMBINED_BY_OR
中使用 2 個(gè)謂詞.
There is no orWhere
in ZF2, but you could use any combination of ZendDbSqlPredicate
objects. For OR use 2 predicates in a PredicateSet
with PredicateSet::COMBINED_BY_OR
.
關(guān)于它的文檔不多,但您可以瀏覽源代碼 - 現(xiàn)在.
There's not much documentation for it, but you could browse the source - for now.
編輯:示例
use ZendDbSqlPredicate;
$select->where(array(
// ...
new PredicatePredicateSet(
array(
new PredicateLike('content', '%'.$searchstring.'%'),
new PredicateLike('title', '%'.$searchstring.'%'),
),
PredicatePredicateSet::COMBINED_BY_OR
),
// ...
));
這篇關(guān)于ZF2 如何 orWhere()的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!