php-curd基类框架

php 最长用的是curd基类,下面收集了一个curd的基类结构.

<?php
/**
 * @uses array_helper
 */
class CRUD_Model extends CI_Base_Model
{
    public $ci;

    public $crud_total = 0;

    private $crud_offset = 0;
    private $crud_page   = 0;
    private $crud_limit  = 20;

    protected $crud_mask = [];

    public $crud_id         = [];
    public $crud_user_id    = 0;
    public $crud_time       = 0;
    public $crud_time_str   = '';
    public $crud_error_code = 0;
    public $crud_has_error  = FALSE;

    public function __construct()
    {
        parent::__construct();
        $this->ci = &get_instance();
    }

    /**
     * ---------------------------------------------------------
     * CRUD Block-separated Methods
     * CRUD 子功能方法
     * ---------------------------------------------------------
     */
    public function crud_paginator($offset = NULL, $page = NULL, $limit = 20)
    {
        // 解析分页参数的方法
        ...
    }

    public function crud_set_error($error_code = 0)
    {
        // 设置错误码(用于API或者Controller判定)和错误标记的方法
        $this->crud_has_error  = TRUE;
        $this->crud_error_code = $error_code;
    }

    protected function crud_process_id($id = 0, $data = [])
    {
        // 处理ID并缓存的方法
        ...
    }

    protected function crud_prepare($data = [])
    {
        // 准备数据(时间戳之类后面需要用到但和主体关系不大的数据)的方法
        $this->crud_time     = time();
        $this->crud_time_str = date('Y-m-d H:i:s', $this->crud_time);
    }

    protected function crud_filter_fields($data = [])
    {
        // 添加和修改时,过滤原数据中不需要的字段的方法
        ...
    }

    protected function crud_save_total($data = [])
    {
        // 查询加上过滤条件后的总条数的方法
        $this->select("''");
        $this->crud_total = $this->count_all_results();
    }

    protected function crud_add_list_filter($data = [])
    {
        // 列表筛选条件过滤方法
    }

    protected function crud_add_detail_filter($data = [])
    {
        // 详情筛选条件过滤方法
    }

    protected function crud_add_fields($data = [])
    {
        // 列表和详情字段附加方法(SELECT子句)
    }

    protected function crud_add_limit()
    {
        // 条数限制(分页过滤器)方法
        if($this->crud_limit !== 'all') {
            $this->limit($this->crud_limit, $this->crud_offset);
        }
    }

    protected function crud_add_order($data = [])
    {
        // 排序附加方法
    }

    protected function crud_add_list_with($data = [])
    {
        // 列表with(CI-Base-Model中额外添加数据的绑定钩子)的附加方法
    }

    protected function crud_add_detail_with($data = [])
    {
        // 详情with的附加方法
    }

    protected function crud_build_item($item = [])
    {
        // 对列表和详情字段进行处理的方法(直接取出来默认是字符串,如果是数字型需要手动转)
        return $item;
    }

    protected function crud_build_proto($data = [])
    {
        // 构造列表分页输出结构的方法,这个之后会向这个数组再附加列表list值
        $proto = [];
        $proto['total']  = ...;
        $proto['offset'] = ...;
        $proto['page']   = ...;
        $proto['is_last_page'] = ...;
        $proto['is_full_page'] = ...;

        return $proto;
    }

    protected function crud_detail_precheck($detail = [])
    {
        // 详情预检查方法
        return TRUE;
    }

    protected function crud_detail_postcheck($detail = [])
    {
        // 详情后期检查方法
        return TRUE;
    }

    protected function crud_insert_precheck($data = [])
    {
        // 新增预检查方法
        return TRUE;
    }

    protected function crud_insert_postcheck($data = [])
    {
        // 新增后期检查方法
        return TRUE;
    }

    protected function crud_update_precheck($data = [])
    {
        // 更新预检查方法
        return TRUE;
    }

    protected function crud_update_postcheck($data = [])
    {
        // 更新后期检查方法
        return TRUE;
    }

    protected function crud_delete_precheck($data = [])
    {
        // 删除预检查方法
        return TRUE;
    }

    protected function crud_delete_postcheck($data = [])
    {
        // 删除后期检查方法
        return TRUE;
    }

    protected function crud_insert_rebuild($data = [])
    {
        // 新增数据时重新构造传入数据的方法(使其符合数据库结构)
        return [];
    }

    protected function crud_update_rebuild($data = [])
    {
        // 更新数据时重新构造传入数据的方法
        return [];
    }

    /**
     * ---------------------------------------------------------
     * CRUD Main Methods
     * CRUD 主要函数
     * ---------------------------------------------------------
     */
    protected function crud_list($offset = NULL, $page = NULL, $limit = 20, $data = [])
    {
        // 查询列表
        ...
    }

    protected function crud_detail($id = 0, $data = [])
    {
        // 查询详情
        ...
    }

    protected function crud_insert($data = [])
    {
        // 添加
        ...
    }

    protected function update_feed($id = 0, $data = [])
    {
        // 修改
        ...
    }

    protected function crud_delete($id = 0)
    {
        // 删除
        ...
    }
}
发布了6 篇原创文章 · 获赞 0 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/manbudezhu/article/details/76944827