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 ...
随机推荐
- IOS页面自动布局 之 NSLayoutConstraint基础篇
使用AutoLayout之前需要知道以下两点: 1.必须设置 translatesAutoresizingMaskIntoConstraints为NO. 2.如果是viewControl则AutoLa ...
- C#设计模式——迭代器模式(Iterator Pattern)
一.概述在软件开发过程中,我们可能会希望在不暴露一个集合对象内部结构的同时,可以让外部代码透明地访问其中包含的元素.迭代器模式可以解决这一问题.二.迭代器模式迭代器模式提供一种方法顺序访问一个集合对象 ...
- jquery实现表格内容筛选
对于表格来说,当数据比较多的时候,我们无法一页一页的查找,这时可以通过一个搜索框来实现搜索. 对于这个搜素框,我们为了更好的体验可以利用keyup事件实现在用户输入的时候就开始筛选,而不是填完以后点击 ...
- .NET开发 正则表达式中的 Bug
又发现了一个 .net 的 bug!最近在使用正则表达式的时候发现:在忽略大小写的时候,匹配值从 0xff 到 0xffff 之间的所有字符,正则表达式竟然也能匹配两个 ASCII 字符:i(code ...
- 重新想象 Windows 8 Store Apps (43) - 多线程之任务: Task 基础, 多任务并行执行, 并行运算(Parallel)
[源码下载] 重新想象 Windows 8 Store Apps (43) - 多线程之任务: Task 基础, 多任务并行执行, 并行运算(Parallel) 作者:webabcd 介绍重新想象 W ...
- ActiveReports 报表应用教程 (9)---交互式报表之动态排序
在 ActiveReports 中除了提供对数据源进行排序的功能之外,还提供了最终用户排序功能,最终用户可以对报表进行区域内排序和整个数据源排序,结合数据钻取.过滤等功能可以让用户更方便地分析报表数据 ...
- JPA(7) spring-data-jpa
对于不是使用spring管理的项目,我们就自己创建对象使用:大概的思路就是①创建dao接口②实现该接口,并且编写逻辑: Dao: public interface StudentDao { publi ...
- 跨平台的 SQL 客户端
The major update to SQL client was to move to the .NET Core networking libraries instead of the nati ...
- bootstrap 学习笔记
bootstrap作为当下的流行框架不知道它怎么能行呢? 之前也看过好多bootstrap的网上教程,可是发现光看真的记不住,说起来也真是忧桑~重点还是要自己做才是真正的印象深刻.后来我发现解析模板是 ...
- IOS GCD 浅析
一.简单介绍 1.队列的类型: 1.1主队列:main queue 主线程队列,更新UI的操作.是一个串行的队列,串行队列每次只处理一个任务. 1.2系统创建的并发队列:glob ...