CodeIgniter3——封装,辅助构建where条件

1 背景

由于codeigniter3的orm实在是太简朴了,要构建复杂where条件时,实在是敲不动。而在tp、larvel等框架中的orm是支持where的便捷构建的。
话不多说,直接上代码。

2 代码

创建/core/MY_Model.php文件:
之后便可以直接对单一的连接或其他独立连接做where的快速构建。

<?php


class MY_Model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}

    /**
     * 辅助建立where语句
     * @param $where '查询条件'
     * @param null $db '可为其他连接的数据库进行条件构建'
     */
    public function helpBuildWhere($where, &$db = null)
    {
        is_null($db) ? $nowDB =& $this->db : $nowDB = $db;
        foreach ($where as $key1 => $val1) {
            $res = preg_match('/^(or_not_group|not_group|or_group|group)\d{0,2}$/', $key1, $match);
            if ($res) {
                /** 需要分组查询条件时,$where中的书写结构。支持多个同类型分组,末尾加上数字
                 * [
                 * 'or_group' => [
                 *  field1 => [ope1, val1],
                 *  field2 => [
                 *      [ope21, val21],
                 *      [ope22, val22]
                 *  ]
                 * ],
                 * 'or_group1' => [],
                 * ]
                 */
                $op = $match[1];
                $fun = $op . "_start";
                $nowDB->$fun();
                    $this->helpBuildWhere($val1, $db);
                $nowDB->group_end();
            } else if (is_array($val1[0])) { // 其第一个元素为array,则为多条件
                /** 单个字段,使用多个查询条件时,$where中的书写结构
                 * [field => [
                 *  [ope1, val],
                 *  [ope2, val]
                 * ]]
                 */
                foreach ($val1 as $val2) {
                    $this->metaOpe($key1, $val2, false, $db);
                }
            } else {
                /** 默认为单条件,$where中的书写结构
                 * [field => [ope, val]]
                 */
                $this->metaOpe($key1, $val1, false, $db);
            }
        }
    }

    /**
     * helpBuildWhere的元操作
     * @param $field '字段名'
     * @param $condition '查询条件'
     * @param false $or '是否为or查询'
     * @param null $db '可为其他连接的数据库进行条件构建'
     */
    public function metaOpe($field, $condition, $or = false, &$db = null)
    {
        is_null($db) ? $nowDB =& $this->db : $nowDB = $db;
        if(is_scalar($condition)) {
            // field => 222 : 默认为等值查询
            $nowDB->where($field, $condition);

        } else if (strpos($condition[0], 'or_') !== false) {
            // 下列所有支持的操作中,加上前缀'or_',即为or查询
            $condition[0] = str_replace('or_', '', $condition[0]);
            $this->metaOpe($field, $condition, true, $db);

        } else if (in_array($condition[0], ['=', '<', '>', '<=', '>=', '<>'])) {
            !$or ? $nowDB->where("{$field} {$condition[0]}", $condition[1]) :
                $nowDB->or_where("{$field} {$condition[0]}", $condition[1]);

        } else if ($condition[0] == 'like') {
            !$or ? $nowDB->like($field, $condition[1], $condition[2] ?? 'both') :
                $nowDB->or_like($field, $condition[1], $condition[2] ?? 'both');

        } else if($condition[0] == 'between') {
            !$or ? $nowDB->where("({$field} between '{$condition[1]}' and '{$condition[2]}')") :
                $nowDB->or_where("({$field} between '{$condition[1]}' and '{$condition[2]}')");

        } else if ($condition[0] == 'in') {
            !$or ? $nowDB->where_in($field, $condition[1]) :
                $nowDB->or_where_in($field, $condition[1]);

        } else if ($condition[0] == 'not_in') {
            !$or ? $nowDB->where_not_in($field, $condition[1]) :
                $nowDB->or_where_not_in($field, $condition[1]);

        } else if ($condition[0] == 'exp') {
            // 表示直接拼接一个查询条件
            $exp = $field." ".$condition[1];
            !$or ? $nowDB->where($exp) : $this->db->or_where($exp);
        }
    }

    public function getPageAndSize($params = [], $defaultPage = 1, $defaultSize = 10)
    {
        $page = isset($params['page']) && !empty($params['page']) ? $params['page'] : $defaultPage;
        $size = isset($params['size']) && !empty($params['size']) ? $params['size'] : $defaultSize;
        return [$page, $size];
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值