用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2669|回复: 5
收起左侧

[讨论/交流] 在AR类中有没有可以实现有数据时更新,无数据时插入?

[复制链接]
发表于 2013-3-13 14:57:18 | 显示全部楼层 |阅读模式
刚看了一下,AR类的,目前还仅有INSERT和UPDATE。如果我有一需求要做到判断该数据是否存在,如不存在就使用insert,若存在就使用update这个方法呢?
要是手动写就做做判断就可以,但CI中有没有自带这个功能的操纵方法呢?
发表于 2013-3-13 15:58:03 | 显示全部楼层
給你參考一下這一段....
PHP复制代码
 
public function save(){
        if(isset($this->id)) {
            return $this->update();
        } else {
            return $this->insert();
        }
 
复制代码

评分

参与人数 1威望 +1 收起 理由
(Lingz)靈斯 + 1 赞一个!

查看全部评分

发表于 2013-3-13 17:34:33 | 显示全部楼层
当然也可以自己写一个公共的model


PHP复制代码
 
 
        //根据条件语句选择插入或更新模型
        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;
                }
        }
 
复制代码

评分

参与人数 1威望 +1 收起 理由
(Lingz)靈斯 + 1 很给力!

查看全部评分

发表于 2013-3-13 18:52:43 | 显示全部楼层
学习一下!
发表于 2013-3-15 13:08:43 | 显示全部楼层
再次上传一下自己平时用到的,写了一个公共model
PHP复制代码
 
<?php
/**
 * 公共方法model
 * @author  xiaozhu
 * 此为公共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;
        }
}
 
复制代码

评分

参与人数 2威望 +12 收起 理由
bax + 2 值得參考
貝殼 + 10 很给力!

查看全部评分

 楼主| 发表于 2013-3-18 15:30:59 | 显示全部楼层
xiaozhuaisnow 发表于 2013-3-15 13:08
再次上传一下自己平时用到的,写了一个公共model

谢谢大哥分享

本版积分规则