CI 框架中 AR 操作
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 操作的更多相关文章
- CI 框架中的自定义路由规则
在 CI 框架中,一个 URL 和它对应的控制器中的类以及类中的方法是一一对应的,如: www.test.com/user/info/zhaoyingnan 其中 user 对应的就是控制器中的 us ...
- php CI框架中URL特殊字符处理与SQL注入隐患
php CI框架中URL特殊字符处理与SQL注入隐患 php CI框架中URL特殊字符有很多是不支持的,导致像c++,括号这些常用的分类,字符都无法正常显示很头痛,而在配置里增加单引号' 反斜杠\ 这 ...
- php json_encode在CI框架中的使用细节
这个错误的造成原因是加载类类库,转换成json格式的时候不熟悉CI框架的规定导致的,CI框架中规定在将数据转换成json格式的时候需要将类库小写,当然了,调用的时候必须保证有这个类库,且可以在对应的文 ...
- CI框架中集成CKEditor编辑器的教程
CKEditor是在很多开发过程中都会用到的一个富文本编辑器,那么如何在CI框架中使用它呢?这里介绍了在CI下使用CKEditor的方法,版本比较低,是在CI 1.7.3下使用fckeditor 2. ...
- CI 框架中的日志处理 以及 404异常处理
最近在整理项目中的日志问题,查了一些关于 “CI 框架中的日志处理 以及 404异常处理” 的东西,顺便记录一下: 关于错误日志: 1. 在CI框架中的 system/core/CodeIgniter ...
- CI框架中的奇葩
今天在win下开发,使用ci框架,本来是没有任何问题,然后转向了mac上开发,结果出现了个奇葩的问题,就是在ci框架中,控制器命名以"Admin_"为前缀的,在url中,控制器也必 ...
- 对CI框架中几个文件libraries
对CI框架中几个文件libraries,helpers,hooks夹说明 来源:未知 时间:2014-10-20 11:37 阅读数:117 作者:xbdadmin [导读] 1.lib ...
- ci框架中model简单的mysql操作
<?php class SingerModel extends CI_Model { function SingerModel() { //会将数据库对象赋值给CI_Controller的db属 ...
- 在CI框架中的配置整合amfphp
之前做的项目用到CI框架和amfphp的整合,主要用于php与flex的交互,在此做一下记录: 一. 安装CI框架: 1. 搭建PHP运行环境,本人在WIN7下用WAMP作测试,安装目录:d:/wa ...
随机推荐
- TCP和UDP之间的区别
TCP---传输控制协议,提供的是面向连接.可靠的字节流服务.当客户和服务器彼此交换数据前,必须先在双方之间建立一个TCP连接,之后才能传输数据.TCP提供超时重发,丢弃重复数据,检验数据,流量控制等 ...
- 正则表达式相关:C# 抓取网页类(获取网页中所有信息)
类的代码: using System; using System.Data; using System.Configuration; using System.Net; using System.IO ...
- Python基础:函数
一.概述 二.声明.定义和调用 三.参数 1.参数传递 2.实参类型 3.形参绑定 四.返回值 五.名字空间与作用域 1.基本概念 2.名字空间 3.作用域 4.总原则 六.高级 1.装饰器 2.生成 ...
- 浅谈ImageList
ImageList组件用了很久,但是一直不太清楚它的实现原理,今天专门特意花了时间倒腾了下,终于弄明白了!于是在这里和大家分享下! 在设计页面中打卡工具箱-组件 找到ImageList组件,将它直接拖 ...
- 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件
[源码下载] 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件 作者:w ...
- [转]基于 Quercus 的手游项目终于上线了
原文:http://blog.andsky.com/quercus-php-ngame/ 经过半年的开发,我们第一款手游戏终于开发完毕,架构使用了 netty + Quercus 实现用 php 通过 ...
- 回文串---Best Reward
HDU 3613 Description After an uphill battle, General Li won a great victory. Now the head of state ...
- php学习笔记:对文件的增删查改等操作
文件的创建: 采用touch()函数,当文件不存在会被创建 例如: <?php header("Content-type: text/html; charset=utf-8" ...
- SQL 语句日期用法及函数
SQL 语句日期用法及函数 --DAY().MONTH().YEAR()——返回指定日期的天数.月数.年数:select day(cl_s_time) as '日' from class --返回天 ...
- android view holder 优化
android 一般都用viewholder来优化contentView,采用sparseArray能够进一步优化 /** * 用法: ImageView bananaView = ViewHolde ...