Model 层中的部分代码

     /**
* CI 中的 AR 操作
* @author zhaoyingnan
**/
public function mAR()
{
/*************** 查询 *************/
//select * from mp4ba limit 21,10;
//$objResult = $this->db->get('mp4ba', 10, 21);
//echo $this->db->last_query();die; //select * from mp4ba where id =32 limit 21,10;
//select * from mp4ba where id =32 and name = '刺客聂隐娘'limit 21,10;
//$objResult = $this->db->get_where('mp4ba', array('id'=>32), 10, 21);
//echo $this->db->last_query();die;
//$objResult = $this->db->get_where('mp4ba', array('id'=>32,'name'=>'刺客聂隐娘'), 10, 21);
//echo $this->db->last_query();die; //select id,name,url from mp4ba where id =32;
//$objResult = $this->db->select('id,name,url')->get_where('mp4ba', array('id'=>32));
//echo $this->db->last_query();die; //select id,name,url from mp4ba where id =32 or id=39;
//$objResult = $this->db->select('id,name,url')->where(array('id'=>32))->or_where(array('id'=>39))->get('mp4ba');
//echo $this->db->last_query();die; //select id,name,url from mp4ba where id in(33,44,55);
//select id,name,url from mp4ba where id in(33,44,55) or sort_id in (3,4);
//select id,name,url from mp4ba where id not in(33,44,55);
//$objResult = $this->db->select('id,name,url')->where_in('id', array(33,44,55))->get('mp4ba');
//$objResult = $this->db->select('id,name,url')->where_in('id', array(33,44,55))->or_where_in('sort_id', array(3,4))->get('mp4ba');
//$objResult = $this->db->select('id,name,url')->where_not_in('id', array(33,44,55))->get('mp4ba');
//echo $this->db->last_query();die; //select id,name,url from mp4ba join user on (mp4ba.uid=user.id) order by mp4ba.dateline desc;
//$objResult = $this->db->select('id,name,url')->from('mp4ba')->join('user', 'mp4ba.uid = user.id')->order_by('mp4ba.dateline', 'desc')->get();
//echo $this->last_query();die; //select * from mp4ba where name like '%刺客%';
//select * from mp4ba where name not like '%刺客%';
//select * from mp4ba where name like '%刺客%' or url like 'eqfdf%';
//$objResult = $this->db->like('name', '刺客')->get('mp4ba');
//$objResult = $this->db->not_like('name', '刺客')->get('mp4ba');
//$objResult = $this->db->like('name', '刺客')->or_like('url', 'eqfdf', 'after')->get('mp4ba');
//echo $this->db->last_query();die; //select max(id) from mp4ba where name = '刺客聂隐娘';
//select min(id) from mp4ba where name = '刺客聂隐娘';
//$objResult = $this->db->select_max('id')->get_where('mp4ba', array('name'=>'刺客聂隐娘'));
//echo $this->db->last_query();die;
//$objResult = $this->db->select_min('id')->get_where('mp4ba', array('name'=>'刺客聂隐娘'));
//echo $this->db->last_query();die; //SELECT id,sort_id,menu,name FROM mp4ba WHERE id > 3 ORDER BY `dateline` desc LIMIT 10,100
//$objResult = $this->db->select('id,sort_id,menu,name')->from('mp4ba')->where('id >', 3)->order_by('dateline desc')->limit(100,10)->get();
//echo $this->db->last_query();
//return $objResult->result(); /*************** 插入 *************/
//生成一条基于你所提供的数据的SQL插入字符串并执行查询。你可以向函数传递 数组 或一个 对象。下面是一个使用数组的例子:
$arInsert = array(
'name' => '小黄人',
'url' => 'www.test.com',
'sort_id' => 1,
'menu' => '动画片'
);
//$this->db->insert('mp4ba', $arInsert);
//echo $this->db->insert_id();die; /*************** 修改 *************/
$arData = array(
'name' => '小黄人,好玩嘛',
'url' => 'www.test_xiaohuangren.com',
'sort_id' => 1,
'menu' => '动画片'
);
//$this->db->update('mp4ba', $arData, array('id'=>3498));
//echo $this->db->affected_rows(); #受影响的行数
//echo '<br/>';
//$objResult = $this->db->where(array('id'=>3498))->get('mp4ba');
//formatOut($objResult->result());die; /*************** 删除 *************/
$this->db->delete('mp4ba', array('id'=>3498));
echo $this->db->affected_rows(); #受影响的行数
}

CI 中 DB_active_rec.php 该类中的部分方法的标注(会持续补充)

 <?php
class CI_DB_active_record
{
/**
* get
* @author zhaoyingnan 2015-10-14 12:50
* @param string $table 操作的表
* @param int $limit limit 值
* @param int $offset offset 值
* @return object
**/
public function get($table = '', $limit = null, $offset = null)
{} /**
* get_where
* @author zhaoyingnan 2015-10-14 12:58
* @param string $table 操作的表
* @param array $where where 子句
* @param int $limit limit 值
* @param int $offset offset 值
* @return object
**/
public function get_where($table = '', $where = null, $limit = null, $offset = null)
{} /**
* select
* @author zhaoyingnan 2015-10-14 13:13
* @param string $select 查询的字段,用逗号隔开
* @param boolean $escape 如果你把它设为FALSE, CodeIgniter 将不会使用反引号保护你的字段或者表名 。这在进行复合查询时很有用。
* @return object
**/
public function select($select = '*', $escape = NULL)
{} /**
* SELECT MAX(field) portion of a query* @description
* @author zhaoyingnan 2015-10-14 13:20
* @param string $select max(field)作用列
* @param string $alias 别名
* @return object
**/
public function select_max($select = '', $alias = '')
{} /**
* SELECT MIN(field) portion of a query
* @author zhaoyingnan 2015-10-14 13:20
* @param string $select min(field)作用列
* @param string $alias 别名
* @return object
**/
public function select_min($select = '', $alias = '')
{} /**
* SELECT AVG(field) portion of a query
* @author zhaoyingnan 2015-10-14 13:20
* @param string $select AVG(field)作用列
* @param string $alias 别名
* @return object
**/
public function select_avg($select = '', $alias = '')
{} /**
* SELECT SUM(field) portion of a query
* @author zhaoyingnan 2015-10-14 13:20
* @param string $select SUM(field)作用列
* @param string $alias 别名
* @return object
**/
public function select_sum($select = '', $alias = '')
{} /**
* @description
* @author zhaoyingnan 2015-10-14 13:26
* @param string $from 表名
* @return object
**/
public function from($from)
{} /**
* where
* @author zhaoyingnan 2015-10-14 13:31
* @param mix $key 传递数组的 key 值
* @param mix $value 传递数组的 key 对应的 value 值
* @return object
**/
public function where($key, $value = NULL, $escape = TRUE)
{
//1,简单的 key/value 方法:
//$this->db->where('name', $name);
//生成: WHERE name = 'Joe' //2.自定义 key/value 方法:
//$this->db->where('name !=', $name);
//$this->db->where('id <', $id);
//生成: WHERE name != 'Joe' AND id < 45 //3.关联数组方法:
//$array = array('name' => $name, 'title' => $title, 'status' => $status);
//$this->db->where($array);
//生成: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'
//使用这个方法时你也可以包含运算符:
//$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date); //4.自定义字符串:
//$where = "name='Joe' AND status='boss' OR status='active'";
//$this->db->where($where);
return $this->_where($key, $value, 'AND ', $escape);
} /**
* where
* @author zhaoyingnan 2015-10-14 13:31
* @param mix $key 传递数组的 key 值
* @param mix $value 传递数组的 key 对应的 value 值
* @return object
**/
public function or_where($key, $value = NULL, $escape = TRUE)
{
//参考where
return $this->_where($key, $value, 'OR ', $escape);
} /**
* where_in
* @author zhaoyingnan 2015-10-14 13:58
* @param string $key 要查询的列
* @param string $values 列的值得范围
* @return object
**/
public function where_in($key = NULL, $values = NULL)
{
return $this->_where_in($key, $values);
} /**
* or_where_in
* @author zhaoyingnan 2015-10-14 13:58
* @param string $key 要查询的列
* @param string $values 列的值得范围
* @return object
**/
public function or_where_in($key = NULL, $values = NULL)
{
return $this->_where_in($key, $values, FALSE, 'OR ');
} /**
* where_not_in
* @author zhaoyingnan 2015-10-14 13:58
* @param string $key 要查询的列
* @param string $values 列的值得范围
* @return object
**/
public function where_not_in($key = NULL, $values = NULL)
{
return $this->_where_in($key, $values, TRUE);
} /**
* or_where_not_in
* @author zhaoyingnan 2015-10-14 13:58
* @param string $key 要查询的列
* @param string $values 列的值得范围
* @return object
**/
public function or_where_not_in($key = NULL, $values = NULL)
{
return $this->_where_in($key, $values, TRUE, 'OR ');
} /**
* order by
* @author zhaoyingnan 2015-10-14 13:35
* @param string $orderby 被排序的列
* @param string $direction asc 或者 desc
* @return object
**/
public function order_by($orderby, $direction = '')
{} /**
* join
* @author zhaoyingnan 2015-10-14 14:07
* @param string $table 表名
* @param string $cond 条件
* @param string $type 指定 JOIN 的类型可选项包括:left, right, outer, inner, left outer, 以及 right outer
* @return
**/
public function join($table, $cond, $type = '')
{} /**
* like
* @author zhaoyingnan 2015-10-14 14:28
* @param stringi $field 错作的列
* @param mix $match 规则
* @param mix $side 通配符(%)位置可用的选项是 'before', 'after' 以及 'both' (这是默认值)
* @return object
**/
public function like($field, $match = '', $side = 'both')
{
return $this->_like($field, $match, 'AND ', $side);
} /**
* not_like
* @author zhaoyingnan 2015-10-14 14:28
* @param stringi $field 错作的列
* @param mix $match 规则
* @param mix $side 通配符(%)位置可用的选项是 'before', 'after' 以及 'both' (这是默认值)
* @return object
**/
public function not_like($field, $match = '', $side = 'both')
{
return $this->_like($field, $match, 'AND ', $side, 'NOT');
} /**
* or_like
* @author zhaoyingnan 2015-10-14 14:28
* @param stringi $field 错作的列
* @param mix $match 规则
* @param mix $side 通配符(%)位置可用的选项是 'before', 'after' 以及 'both' (这是默认值)
* @return object
**/
public function or_like($field, $match = '', $side = 'both')
{
return $this->_like($field, $match, 'OR ', $side);
} /**
* or_not_like
* @author zhaoyingnan 2015-10-14 14:28
* @param stringi $field 错作的列
* @param mix $match 规则
* @param mix $side 通配符(%)位置可用的选项是 'before', 'after' 以及 'both' (这是默认值)
* @return object
**/
public function or_not_like($field, $match = '', $side = 'both')
{
return $this->_like($field, $match, 'OR ', $side, 'NOT');
} /**
* insert 单条插入
* @author zhaoyingnan 2015-10-14 14:52
* @param string $table 表名
* @param array $set 关联数组
* @return object
**/
function insert($table = '', $set = NULL)
{} /**
* insert 批量出入
* @author zhaoyingnan 2015-10-14 14:52
* @param string $table 表名
* @param array $set 关联数组
* @return object
**/
public function insert_batch($table = '', $set = NULL)
{} /**
* update
* @author zhaoyingnan 2015-10-14 15:02
* @param string $table 表名
* @param array $set 修改内容的关联数组
* @param mixed $where where条件
* @return object
**/
public function update($table = '', $set = NULL, $where = NULL, $limit = NULL)
{} /**
* delete
* @author zhaoyingnan 2015-10-14 15:12
* @param mix $table 表名
* @param mixed $where where条件
* @return object
**/
public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE)
{
//第一个参数是表名,第二个参数是where子句。你可以不传递第二个参数,使用 where() 或者 or_where() 函数来替代它:
//$this->db->where('id', $id);
//$this->db->delete('mytable'); //如果你想要从一个以上的表中删除数据,你可以将一个包含了多个表名的数组传递给delete()函数。
//$tables = array('table1', 'table2', 'table3');
//$this->db->where('id', '5');
//$this->db->delete($tables); //如果你想要删除表中的全部数据,你可以使用 truncate() 函数,或者 empty_table() 函数。
} /**
* limit
* @author zhaoyingnan 2015-10-14 14:34
* @param int $value
* @param int $offset
* @return object
**/
public function limit($value, $offset = '')
{} /**
* Where
*
* Called by where() or or_where()
*
* @param mixed
* @param mixed
* @param string
* @return object
**/
protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL)
{} /**
* Like
*
* Called by like() or orlike()
*
* @param mixed
* @param mixed
* @param string
* @return object
**/
protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '')
{
if ( ! is_array($field))
{
$field = array($field => $match);
} foreach ($field as $k => $v)
{
$k = $this->_protect_identifiers($k); $prefix = (count($this->ar_like) == 0) ? '' : $type; $v = $this->escape_like_str($v); if ($side == 'none')
{
$like_statement = $prefix." $k $not LIKE '{$v}'";
}
elseif ($side == 'before')
{
$like_statement = $prefix." $k $not LIKE '%{$v}'";
}
elseif ($side == 'after')
{
$like_statement = $prefix." $k $not LIKE '{$v}%'";
}
else
{
$like_statement = $prefix." $k $not LIKE '%{$v}%'";
} // some platforms require an escape sequence definition for LIKE wildcards
if ($this->_like_escape_str != '')
{
$like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr);
} $this->ar_like[] = $like_statement;
if ($this->ar_caching === TRUE)
{
$this->ar_cache_like[] = $like_statement;
$this->ar_cache_exists[] = 'like';
} }
return $this;
} }

CI 框架中 AR 操作的更多相关文章

  1. CI 框架中的自定义路由规则

    在 CI 框架中,一个 URL 和它对应的控制器中的类以及类中的方法是一一对应的,如: www.test.com/user/info/zhaoyingnan 其中 user 对应的就是控制器中的 us ...

  2. php CI框架中URL特殊字符处理与SQL注入隐患

    php CI框架中URL特殊字符处理与SQL注入隐患 php CI框架中URL特殊字符有很多是不支持的,导致像c++,括号这些常用的分类,字符都无法正常显示很头痛,而在配置里增加单引号' 反斜杠\ 这 ...

  3. php json_encode在CI框架中的使用细节

    这个错误的造成原因是加载类类库,转换成json格式的时候不熟悉CI框架的规定导致的,CI框架中规定在将数据转换成json格式的时候需要将类库小写,当然了,调用的时候必须保证有这个类库,且可以在对应的文 ...

  4. CI框架中集成CKEditor编辑器的教程

    CKEditor是在很多开发过程中都会用到的一个富文本编辑器,那么如何在CI框架中使用它呢?这里介绍了在CI下使用CKEditor的方法,版本比较低,是在CI 1.7.3下使用fckeditor 2. ...

  5. CI 框架中的日志处理 以及 404异常处理

    最近在整理项目中的日志问题,查了一些关于 “CI 框架中的日志处理 以及 404异常处理” 的东西,顺便记录一下: 关于错误日志: 1. 在CI框架中的 system/core/CodeIgniter ...

  6. CI框架中的奇葩

    今天在win下开发,使用ci框架,本来是没有任何问题,然后转向了mac上开发,结果出现了个奇葩的问题,就是在ci框架中,控制器命名以"Admin_"为前缀的,在url中,控制器也必 ...

  7. 对CI框架中几个文件libraries

    对CI框架中几个文件libraries,helpers,hooks夹说明 来源:未知    时间:2014-10-20 11:37   阅读数:117   作者:xbdadmin [导读] 1.lib ...

  8. ci框架中model简单的mysql操作

    <?php class SingerModel extends CI_Model { function SingerModel() { //会将数据库对象赋值给CI_Controller的db属 ...

  9. 在CI框架中的配置整合amfphp

    之前做的项目用到CI框架和amfphp的整合,主要用于php与flex的交互,在此做一下记录: 一. 安装CI框架: 1.  搭建PHP运行环境,本人在WIN7下用WAMP作测试,安装目录:d:/wa ...

随机推荐

  1. for循环的嵌套,for循环的穷举迭代

    for循环的嵌套            输入一个正整数,求阶乘的和 嵌套            Console.Write("请输入一个正整数:");            int ...

  2. csharp:引入app.manifest,程序在win7下以管理员权限运行配置方法

    https://msdn.microsoft.com/en-us/library/windows/desktop/hh848036(v=vs.85).aspx https://msdn.microso ...

  3. 后缀数组---New Distinct Substrings

    Description Given a string, we need to find the total number of its distinct substrings. Input T- nu ...

  4. Android应用开发基础之六:页面跳转和数据传递

    创建第二个Activity 需要在清单文件中为其配置一个activity标签 标签中如果带有这个子节点,则会在系统中多创建一个快捷图标 <intent-filter> <action ...

  5. 趣味题:恺撒Caesar密码(c++实现)

    描述:Julius Caesar 生活在充满危险和阴谋的年代.为了生存,他首次发明了密码,用于军队的消息传递.假设你是Caesar 军团中的一名军官,需要把Caesar 发送的消息破译出来.并提供给你 ...

  6. swift学习笔记之-枚举

    //枚举定义 import UIKit //枚举定义 (枚举.结构体.类的名字首字母必须大写,表示为定义了新的类型) /*枚举为一组相关的值定义了一个共同的类型,使你可以在你的代码中以类型安全的方式来 ...

  7. 删除src值为空的img标签

    今天刚刚完成了一个官网的前后台整站建设,虽然不是很复杂,但感觉获益良多.由于涉及到一点后台问题,所以期间遇到了不少问题.学到的东西,得作个总结.今天先讲讲img的路径问题.由于现在很多网站喜欢全屏大图 ...

  8. 为网站添加ico图标

    1.将目标图片装换为.ico格式 在线转换工具:http://www.bitbug.net/ 2.将装换后的图标放入项目文件中,一般命名为favicon 3.在项目文件的index页面中引入 < ...

  9. UITabBarController QQ

    AppDelegate.m #import "AppDelegate.h" #import "FirstViewController.h" #import &q ...

  10. Android源码分析之AsyncTask

    AsyncTask相信从事Android开发的同学都不陌生,基本都应该用到了,和以前一样我们还是先来看看此类的summary.AsyncTask 可以确保更合理.容易的使用UI线程.这个类是设计用来执 ...