(Lingz)靈斯 发表于 2013-3-13 14:57:18

在AR类中有没有可以实现有数据时更新,无数据时插入?

刚看了一下,AR类的,目前还仅有INSERT和UPDATE。如果我有一需求要做到判断该数据是否存在,如不存在就使用insert,若存在就使用update这个方法呢?
要是手动写就做做判断就可以,但CI中有没有自带这个功能的操纵方法呢?

bax 发表于 2013-3-13 15:58:03

給你參考一下這一段....

public function save(){
      if(isset($this->id)) {
            return $this->update();
      } else {
            return $this->insert();
      }

xiaozhuaisnow 发表于 2013-3-13 17:34:33

当然也可以自己写一个公共的model




      //根据条件语句选择插入或更新模型
        function update_model_forwhere($where='',$table,$data)
        {
                $this->db->where($where);
                $enterprise_num=$this->db->count_all_results($table);
                $query=$this->db->where($where);
                if ($enterprise_num>0)
                {
                        $this->db->update($table,$data);
                        return true;
                }
                else
                {
                        $this->db->insert($table,$data);
                        return false;
                }
        }

smallhe 发表于 2013-3-13 18:52:43

学习一下!

xiaozhuaisnow 发表于 2013-3-15 13:08:43

再次上传一下自己平时用到的,写了一个公共model

<?php
/**
* 公共方法model
* @authorxiaozhu
* 此为公共model 请勿添加私有model
*/
class Public_mdl extends MY_Model{
       
        function __construct(){
                parent::__construct();
        }

        /* 添加数据并返回插入数据时的id
       * 补充:xiaozhu
       * 如果insert_id 的对象没有设置id为AUTO_INCREMENT
       * 那么将return 0
       * */
        public function insert_id($table,$data)
        {
                $this->db->insert($table,$data);
                return $this->db->insert_id(); //id必须为自增
        }
       
        /*
       * 根据条件语句更新数据模型
       * code xiaozhu
       * */
        function update_model_forwhere($where='',$table,$data)
        {
                $this->db->where($where);
                $enterprise_num=$this->db->count_all_results($table);
                $query=$this->db->where($where);
                if ($enterprise_num>0)
                {
                        $this->db->update($table,$data);
                        return true;
                }
                else
                {
                        $this->db->insert($table,$data);
                        return false;
                }
        }
       
        /*
       * 数据插入数据模型
       * code xiaozhu
       * */
        function insert_model_forwhere($table,$data)
        {
                        $this->db->insert($table,$data);
                        return true;
        }
       
        /*
       * 根据条件语句删除模型
       * code xiaozhu
       * */
        function delete_model_forwhere($where,$table)
        {
                $this->db->where($where);
                $enterprise_num=$this->db->count_all_results($table);
                if ($enterprise_num>0 && !empty($where))
                {
                        $this->db->delete($table, $where);
                        return true;
                }
                else
                {
               
                        return false;
                       
                }
        }
       
        /*
       * @综合条件查询语句模型
       * @where array or string
       * @table 表名
       * @order_by_value 要排序的表字段
       * @order_by_sort desc or asc
       * @limit 取几条数据
       * @offet 隔几条
       * @code xiaozhu
       * */
        function get_model_forwhere($where='',$table,$order_by_value='',$order_by_sort='',$limit='',$offet='')
        {
                if (!empty($where))$this->db->where($where);
                $enterprise_num=$this->db->count_all_results($table);
                if ($enterprise_num>0)
                {
                        //if (!empty($order_by))$this->db->order_by("title", "desc");
                        if (!empty($where)){
                                if (!empty($order_by_value) && !empty($order_by_sort))$this->db->order_by($order_by_value, $order_by_sort);
                                if (!empty($limit)){
                                        if (!empty($offet)){
                                                $query=$this->db->get_where($table,$where,$limit,$offet);
                                        }else{
                                                $query=$this->db->get_where($table,$where,$limit);
                                        }
                                }else{
                                        $query=$this->db->get_where($table,$where);
                                }
                               
                        }else{
                                if (!empty($order_by_value) && !empty($order_by_sort))$this->db->order_by($order_by_value, $order_by_sort);
                                if (!empty($limit)){
                                        //$query=$this->db->get_where($table,$where,$limit);
                                        //$query=$this->db->get($table,$limit);
                                        if (!empty($offet)){
                                                $query=$this->db->get($table,$limit,$offet);
                                        }else{
                                                $query=$this->db->get($table,$limit);
                                        }
                                }else{
                                        $query=$this->db->get($table);
                                }
                               
                        }
                        $some_msg=array();
                        foreach ($query->result_array() as $rows)
                        {
                                $some_msg[]=$rows;
                        }
                        //print_r($some_msg);
                        return $some_msg;
                }
                else
                {
               
                        return false;
                       
                }
        }
       
        /*
       * @根据条件语句返回符合条件数据条数
       * @where array类型 也可为字符串类型
       * @array('id'=>'something')
       * @string id,'something'
       * @code xiaozhu
       * */
        function get_count($where='',$table){
               
                if (!empty($where))$this->db->where($where);
                $count=$this->db->count_all_results($table);
                return $count;
        }
}

(Lingz)靈斯 发表于 2013-3-18 15:30:59

xiaozhuaisnow 发表于 2013-3-15 13:08 static/image/common/back.gif
再次上传一下自己平时用到的,写了一个公共model

谢谢大哥分享{:1_1:}
页: [1]
查看完整版本: 在AR类中有没有可以实现有数据时更新,无数据时插入?