Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

php - ZF2 How to orWhere()

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.

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;

the lines with the $select->where->like are the ones that are ANDed and I want them with OR. What should I change?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

There's not much documentation for it, but you could browse the source - for now.

Edit: sample

use ZendDbSqlPredicate;

$select->where(array(
    // ...
    new PredicatePredicateSet(
        array(
            new PredicateLike('content', '%'.$searchstring.'%'),
            new PredicateLike('title', '%'.$searchstring.'%'),
        ),
        PredicatePredicateSet::COMBINED_BY_OR
    ),
    // ...
));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...