CI的model层的操作

1.需求

整理ci框架下model层的相关操作

2.代码

model的代码,放在application/model目录下,文件名为Coupon.php

<?php

class Coupon extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    /*
     * @param array 一条记录数组
     * @return bool 成功返回true
     * */

    public function insert($array)
    {

        $result = $this->db->insert('coupon',$array);
        return $result;
    }
    /*
     *  更新某个字段,$increase=true时,表示字段=字段+
     *  @return int 成功返回true,失败返回false
     *
     * */
    public function update_field($field,$value,$wheresql,$increase=false)
    {
        if(count($wheresql)==0)
            return 0;

        $sql = "update ci_coupon set ".$field."=";

        if($increase===true){
            $sql.=$field.'+';
        }

        $sql.=$this->db->escape($value);
        $sql .=" where ";
        foreach ($wheresql as $key => $val){
            $sql.=$key.$this->db->escape($val);
        }
        $query=$this->db->query($sql);
        return ($this->db->affected_rows()?true:false);
    }

    /*
     * 根据券id删除单条记录
     * @param string 券id
     * @return int 成功返回true,失败返回false
     * */
    public function delete($couponId)
    {
        $this->db->where('Quan_id', $couponId);
        $this->db->delete('ci_coupon');
        return ($this->db->affected_rows()?true:false);
    }

    /*
     * 判断记录是否存在
     * @return bool 存在返回true,不存在返回false,
     */
    public function exists($field,$value)
    {
        $this->db->where($field, $value);
        $this->db->from('coupon');
        return $this->db->count_all_results()==0?false:true;
    }

    /*
     * 根据一个条件获得数据
     * @return array 有找到返回结果数组,否则返回空数组
     */
    public function get_where($onewhere)
    {
        if(count($onewhere)==0)
        {
            return array();
        }
        $result = $this->get_list($onewhere);
        return $result;
    }

    /*
     * 获得单条记录单字段
     * @return string
     */
    public function get_field($wheresql,$field='')
    {
        $result=$this->get_list($wheresql,$field,1,'');
        if(count($result)==0)
            return '';

        return $result[0]->$field;
    }

    /*
     * 根据多个条件获得数据
     */
    public function get_list($wheresql,$field='*',$limit=-1,$orderby='')
    {
        if (count($wheresql) == 0) {
            return array();
        }

        $sql = "SELECT " . $field . " FROM ci_coupon WHERE ";

        foreach ($wheresql as $key => $val ) {
            if (isset($key) && (0 < strlen($key))) {
                $sql .= $key;
            }

            if (isset($val) && (0 < strlen($val))) {
                $sql .= $this->db->escape($val);
            }
        }

        if ($orderby != "") {
            $sql .= " order by " . $orderby;
        }

        if ($limit != -1) {
            $sql .= " limit " . $limit;
        }

        $query = $this->db->query($sql);
        return $query->result();
    }


    /*
     * 根据多个条件获得分页数据
     */
    public function get_list_page($search,$field='*',$pagesize=200,$pageindex=1,$orderby='',&$count=0)
    {
        if ($pageindex <= 0) {
            $pageindex = 1;
        }

        if ($pagesize <= 0) {
            $pagesize = 200;
        }

        if (count($search) == 0) {
            return array();
        }

        $sql = "SELECT " . $field . " FROM ci_coupon WHERE ";
        $wheresql = "";

        foreach ($search as $key => $val ) {
            if (isset($key) && (0 < strlen($key))) {
                $wheresql .= $key;
            }

            if (isset($val) && (0 < strlen($val))) {
                $wheresql .= $this->db->escape($val);
            }

        }

        $sql .= $wheresql;

        if ($orderby != "") {
            $sql .= " order by " . $orderby;
        }

        $sql .= " limit " . (($pageindex - 1) * $pagesize) . "," . $pagesize;
        $query = $this->db->query($sql);
        $result = $query->result();
        $cquery = $this->db->query("SELECT count(0) as counts FROM ci_coupon WHERE " . $wheresql);
        $cresult = $cquery->result();
        $count = $cresult[0]->counts;
        return $result;
    }

}

3.控制器调用实例

public function __construct()
    {
        parent::__construct();
        $this->load->model("coupon");
    }
$result = $this->coupon->get_list_page(array('Quan_endtime<='=>time()),'Quan_id');

 

posted @ 2017-02-05 14:51  layfork  阅读(4053)  评论(0编辑  收藏  举报