lony 发表于 2008-11-17 10:14:16

求高手:如果用this->db->or_like(),如何加括号?

在搜索里,经常很多条件查询,会很多or_like()

但是用这个的话,and del=0 往往就失效了。

必须要写sql语句加括号才能解决吗?

Hex 发表于 2008-11-17 17:52:52

貌似是要自己写 SQL 解决多层括号问题,AR 只是替你完成一些简单的 SQL。

lony 发表于 2008-11-18 17:28:45

O L .
谢谢HEX。。。。。

sam 发表于 2008-11-18 18:59:07

写复杂的sql,干脆别用ar

hello3721 发表于 2010-3-2 18:02:30

本帖最后由 hello3721 于 2010-3-2 18:05 编辑

很好解决:
给like数组加上括号即可。
修改system\database\DB_active_rec.php
约第1559行
                if (count($this->ar_like) > 0)
                {
                        if (count($this->ar_where) > 0)
                        {
                                $sql .= "\nAND ";
                        }

                        $sql .= implode("\n", $this->ar_like);

                }
改为
                if (count($this->ar_like) > 0)
                {
                        if (count($this->ar_where) > 0)
                        {
                                $sql .= "\nAND (";
                        }

                        $sql .= implode("\n", $this->ar_like);
                       
                        if (count($this->ar_where) > 0)
                        {
                                $sql .= ")";
                        }
                }
希望CI开发者尽快修补or_like无法内部形成括号的BUG
这个论坛很脑残,显示回车符\n会掉一个斜杠,懂PHP的自己加上。

haileCI 发表于 2012-8-29 17:14:10

本帖最后由 haileCI 于 2012-8-29 17:56 编辑

public function or_like_open()
    {
      $open='(';
      $prefix = (count($this->ar_like) == 0) ? '' : ' OR ';
      $this->ar_like[] = $prefix.$open;
      if ($this->ar_caching === true)
      {
         $this->ar_cache_like[] = $prefix.$open;
         $this->ar_cache_exists[] = 'like';
      }
      
    }
   
    public function or_like_close()
    {
      $close=')';
      $this->ar_like[] =$close;
      if ($this->ar_caching === true)
      {
         $this->ar_cache_like[] = $close;
         $this->ar_cache_exists[] = 'like';
      }
    }


   public function like_open()
    {
      $open='(';
      $prefix = (count($this->ar_like) == 0) ? '' : ' AND ';
      $this->ar_like[] = $prefix.$open;
      if ($this->ar_caching === true)
      {
         $this->ar_cache_like[] = $prefix.$open;
         $this->ar_cache_exists[] = 'like';
      }
      
    }
   
    public function like_close()
    {
      $close=')';
      $this->ar_like[] =$close;
      if ($this->ar_caching === true)
      {
         $this->ar_cache_like[] = $close;
         $this->ar_cache_exists[] = 'like';
      }
    }

在DB指定类中加入上面几个个函数
在protected function _like()
中 的适当
倒数几行中加入 即可
if(trim(end($this->ar_like))=='OR (' OR trim(end($this->ar_like))=='(')
            {
                $like_statement=trim($like_statement,$prefix);
            }
            if(trim(end($this->ar_like))=='AND (' OR trim(end($this->ar_like))=='(')
            {
                $like_statement=trim($like_statement,$prefix);
            }
使用的时候先 $this->db->or_like_open()   //类似 or (
               //这里是很多or_like
   or_like 结束时候 用 $this->db->or_like_close();类似 )
页: [1]
查看完整版本: 求高手:如果用this->db->or_like(),如何加括号?