think php TP框架中where()方法代码增强 支持where()中多个字符串条件连接写

浏览:6505 发布日期:2013/12/14 分类:技术分享 关键字: think php where
TP框架中where()方法代码增强 。支持where()中多个字符串条件连贯写。
现在支持多个where连操作,但是不支持类似:$model->where("a=$b")->where("b='$c'")这样条件。修改2行。让我们可以更爽的支持随心所欲的where()  /**
     * 指定查询条件 支持任意多个where 连贯操作
     * @access public
     * @param mixed $where 条件表达式
     * @param mixed $parse 预处理参数
     * @return Model
     */
    public function where ( $where , $parse = null )
    {
        if (! is_null($parse) && is_string($where))
        {
            if (! is_array($parse))
            {
                $parse = func_get_args();
                array_shift($parse);
            }
            $parse = array_map(array($this->db,'escapeString'), $parse);
            $where = vsprintf($where, $parse);
        } elseif (is_object($where))
        {
            $where = get_object_vars($where);
        }
        if (is_string($where) && '' != $where)
        {
            $map = array();
            $oldWhere=$this->options['where']['_string'] ;
            if($oldWhere)
            {
                $where=$oldWhere.' and '.$where; //此处代码增强,让字符串条件可以多个
            }
            $map['_string'] = $where;
            $where = $map;
        }
        
        if (isset($this->options['where']))
        {
            $this->options['where'] = array_merge($this->options['where'], $where);
        } else
        {
            $this->options['where'] = $where;
        }   
        return $this;
    }
//记得,原think php代码的任何代码不要变。自己建立一个CommonModel继承 TP的model在自己的项目中。用来专门增强TP没有而实际开发中需要的功能。然后继承自己的CommonModel后可以大大减少代码量和代码的复杂度。
最佳答案
评论( 相关
后面还有条评论,点击查看>>