Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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
    ),
    // ...
));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...